Hướng dẫn how do i check if a string is none in python? - làm cách nào để kiểm tra xem một chuỗi không có trong python?

Tôi thấy mã hóa cứng (sic) "" Mỗi lần kiểm tra một chuỗi trống không tốt.

Phương pháp tiếp cận mã sạch

Làm điều này: foo == "" là thực hành rất tệ. "" là một giá trị kỳ diệu. Bạn không bao giờ nên kiểm tra chống lại các giá trị ma thuật (thường được gọi là số ma thuật)

Những gì bạn nên làm là so sánh với một tên biến mô tả.

Tên biến mô tả

Người ta có thể nghĩ rằng "trống_string" là một tên biến mô tả. Nó không phải là.It isn't.

Trước khi bạn đi và làm empty_string = "" và nghĩ rằng bạn có một tên biến tuyệt vời để so sánh với. Đây không phải là những gì "Tên biến mô tả" có nghĩa là.

Một tên biến mô tả tốt dựa trên bối cảnh của nó. Bạn phải suy nghĩ về chuỗi trống là gì.is.

  • Nó đến từ đâu.
  • Tại sao nó ở đó.
  • Tại sao bạn cần kiểm tra nó.

Ví dụ trường mẫu đơn giản

Bạn đang xây dựng một biểu mẫu nơi người dùng có thể nhập các giá trị. Bạn muốn kiểm tra xem người dùng có viết gì đó hay không.

Một tên biến tốt có thể là

if csvfield == CSV_NONE:
    csvfield = None
0

Điều này làm cho mã rất dễ đọc

if formfields.name == not_filled_in:
    raise ValueError("We need your name")

Ví dụ phân tích cú pháp CSV kỹ lưỡng

Bạn đang phân tích các tệp CSV và muốn chuỗi trống được phân tích cú pháp là

if csvfield == CSV_NONE:
    csvfield = None
1

(Vì CSV hoàn toàn dựa trên văn bản, nên nó không thể biểu thị

if csvfield == CSV_NONE:
    csvfield = None
1 mà không sử dụng từ khóa được xác định trước)

Một tên biến tốt có thể là

if csvfield == CSV_NONE:
    csvfield = None
3

Điều này làm cho mã dễ dàng thay đổi và điều chỉnh nếu bạn có tệp CSV mới đại diện cho

if csvfield == CSV_NONE:
    csvfield = None
1 với một chuỗi khác so với ""

if csvfield == CSV_NONE:
    csvfield = None

Không có câu hỏi về việc nếu đoạn mã này là chính xác. Rõ ràng là nó làm những gì nó nên làm.

So sánh điều này với

if csvfield == EMPTY_STRING:
    csvfield = None

Câu hỏi đầu tiên ở đây là, tại sao chuỗi trống xứng đáng được đối xử đặc biệt?

Điều này sẽ nói với các lập trình viên tương lai rằng một chuỗi trống phải luôn luôn được coi là

if csvfield == CSV_NONE:
    csvfield = None
1.

Điều này là do nó kết hợp logic kinh doanh (giá trị CSV nên là

if csvfield == CSV_NONE:
    csvfield = None
1) với việc triển khai mã (chúng ta thực sự so sánh với điều gì)

Cần phải có một sự tách biệt của mối quan tâm giữa hai người.

Chuỗi trống có bằng không không?
Let’s see different methods of checking if string is empty or not.

Một chuỗi trống là một thể hiện chuỗi có độ dài bằng không, trong khi một chuỗi null không có giá trị nào cả. Một chuỗi trống được biểu diễn là "". Đó là một chuỗi ký tự của các ký tự bằng không. Một chuỗi null được biểu diễn bởi null.

Chuỗi Python là bất biến và do đó có khả năng xử lý phức tạp hơn khi nói về các hoạt động của nó. Lưu ý rằng một chuỗi có khoảng trắng thực sự là một chuỗi trống nhưng có kích thước không khác. Bài viết này cũng đã thảo luận về vấn đề và giải pháp cho nó. & NBSP; Hãy để xem các phương pháp khác nhau để kiểm tra xem chuỗi có trống hay không.

Python3

if csvfield == CSV_NONE:
    csvfield = None
8
if csvfield == CSV_NONE:
    csvfield = None
9 ""

if csvfield == EMPTY_STRING:
    csvfield = None
1
if csvfield == CSV_NONE:
    csvfield = None
9
if csvfield == EMPTY_STRING:
    csvfield = None
3

if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
if csvfield == EMPTY_STRING:
    csvfield = None
6
if csvfield == EMPTY_STRING:
    csvfield = None
7
if csvfield == CSV_NONE:
    csvfield = None
9
if csvfield == EMPTY_STRING:
    csvfield = None
9

if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
if csvfield == EMPTY_STRING:
    csvfield = None
6""55____19
if csvfield == EMPTY_STRING:
    csvfield = None
9

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
2

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
3
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
4

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
2

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
0
if csvfield == EMPTY_STRING:
    csvfield = None
5empty_string = ""0
if csvfield == CSV_NONE:
    csvfield = None
41
if csvfield == CSV_NONE:
    csvfield = None
42 empty_string = ""0
if csvfield == CSV_NONE:
    csvfield = None
98

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
2

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
2

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
3
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
4

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
2

if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
22____855
if csvfield == CSV_NONE:
    csvfield = None
9
if csvfield == EMPTY_STRING:
    csvfield = None
9
 
 

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
0
if csvfield == EMPTY_STRING:
    csvfield = None
5empty_string = ""0
if csvfield == CSV_NONE:
    csvfield = None
65
if csvfield == CSV_NONE:
    csvfield = None
42 empty_string = ""0
if csvfield == EMPTY_STRING:
    csvfield = None
23

Đầu ra: & nbsp; & nbsp;

Python3

if csvfield == CSV_NONE:
    csvfield = None
8
if csvfield == CSV_NONE:
    csvfield = None
9 ""

if csvfield == EMPTY_STRING:
    csvfield = None
1
if csvfield == CSV_NONE:
    csvfield = None
9
if csvfield == EMPTY_STRING:
    csvfield = None
3

if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
if csvfield == EMPTY_STRING:
    csvfield = None
6""55____19
if csvfield == EMPTY_STRING:
    csvfield = None
9

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
0
if csvfield == EMPTY_STRING:
    csvfield = None
5empty_string = ""0
if csvfield == CSV_NONE:
    csvfield = None
41
if csvfield == CSV_NONE:
    csvfield = None
42 empty_string = ""0
if csvfield == CSV_NONE:
    csvfield = None
98

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
2

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
3
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
4

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
2

if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
22____855
if csvfield == CSV_NONE:
    csvfield = None
9
if csvfield == EMPTY_STRING:
    csvfield = None
9

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
0
if csvfield == EMPTY_STRING:
    csvfield = None
5empty_string = ""0
if csvfield == CSV_NONE:
    csvfield = None
65
if csvfield == CSV_NONE:
    csvfield = None
42 empty_string = ""0
if csvfield == EMPTY_STRING:
    csvfield = None
23

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
2

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
3
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
4

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
2

if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
22____855
if csvfield == CSV_NONE:
    csvfield = None
9
if csvfield == EMPTY_STRING:
    csvfield = None
9
  

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
0
if csvfield == EMPTY_STRING:
    csvfield = None
5empty_string = ""0
if csvfield == CSV_NONE:
    csvfield = None
65
if csvfield == CSV_NONE:
    csvfield = None
42 empty_string = ""0
if csvfield == EMPTY_STRING:
    csvfield = None
23

Đầu ra: & nbsp; & nbsp;
 

Phương pháp: Sử dụng danh sách hiểu & nbsp;

if csvfield == CSV_NONE:
    csvfield = None
8
if csvfield == CSV_NONE:
    csvfield = None
9 ""

if csvfield == EMPTY_STRING:
    csvfield = None
1
if csvfield == CSV_NONE:
    csvfield = None
9
if csvfield == EMPTY_STRING:
    csvfield = None
3

if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
if csvfield == EMPTY_STRING:
    csvfield = None
6""55____19
if csvfield == EMPTY_STRING:
    csvfield = None
9

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
0
if csvfield == EMPTY_STRING:
    csvfield = None
5empty_string = ""0
if csvfield == CSV_NONE:
    csvfield = None
41
if csvfield == CSV_NONE:
    csvfield = None
42 empty_string = ""0
if csvfield == CSV_NONE:
    csvfield = None
98

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
2

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
3
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
4

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
2

if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
22____855
if csvfield == CSV_NONE:
    csvfield = None
9
if csvfield == EMPTY_STRING:
    csvfield = None
9

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
0
if csvfield == EMPTY_STRING:
    csvfield = None
5empty_string = ""0
if csvfield == CSV_NONE:
    csvfield = None
65
if csvfield == CSV_NONE:
    csvfield = None
42 empty_string = ""0
if csvfield == EMPTY_STRING:
    csvfield = None
23

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
2

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
3
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
4

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
2

Output:    

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes

Phương pháp số 4: Sử dụng không + str.isspace & nbsp;

Hoạt động theo cách tương tự như phương thức trên và kiểm tra các khoảng trống trong chuỗi. Phương pháp này hiệu quả hơn bởi vì, dải () yêu cầu thực hiện hoạt động dải cũng có tải tính toán, nếu không. không gian có số lượng tốt.

Python3

if csvfield == CSV_NONE:
    csvfield = None
8
if csvfield == CSV_NONE:
    csvfield = None
9 ""

if csvfield == EMPTY_STRING:
    csvfield = None
1
if csvfield == CSV_NONE:
    csvfield = None
9
if csvfield == EMPTY_STRING:
    csvfield = None
3

if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
if csvfield == EMPTY_STRING:
    csvfield = None
6""55____19
if csvfield == EMPTY_STRING:
    csvfield = None
9

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
0
if csvfield == EMPTY_STRING:
    csvfield = None
5empty_string = ""0
if csvfield == CSV_NONE:
    csvfield = None
41
if csvfield == CSV_NONE:
    csvfield = None
42 empty_string = ""0
if csvfield == CSV_NONE:
    csvfield = None
98

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
2

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
3
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
4

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
2

if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
22____855
if csvfield == CSV_NONE:
    csvfield = None
9
if csvfield == EMPTY_STRING:
    csvfield = None
9

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
0
if csvfield == EMPTY_STRING:
    csvfield = None
5empty_string = ""0
if csvfield == CSV_NONE:
    csvfield = None
65
if csvfield == CSV_NONE:
    csvfield = None
42 empty_string = ""0
if csvfield == EMPTY_STRING:
    csvfield = None
23

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
1
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
2

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
3
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
4

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
8
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : No
2

if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
5
The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes
22____855
if csvfield == CSV_NONE:
    csvfield = None
9
if csvfield == EMPTY_STRING:
    csvfield = None
9
 
 

The zero length string without spaces is empty ? : Yes
The zero length string with just spaces is empty ? : Yes

The zero length string without spaces is empty ? : Yes The zero length string with just spaces is empty ? : No0if csvfield == EMPTY_STRING: csvfield = None 5empty_string = ""0 if csvfield == CSV_NONE: csvfield = None 65if csvfield == CSV_NONE: csvfield = None 42 empty_string = ""0 if csvfield == EMPTY_STRING: csvfield = None 23

Python3

if csvfield == EMPTY_STRING:
    csvfield = None
36
if csvfield == CSV_NONE:
    csvfield = None
9""

Đầu ra: & nbsp; & nbsp;

if csvfield == EMPTY_STRING:
    csvfield = None
4
if csvfield == EMPTY_STRING:
    csvfield = None
51


Phương pháp: Sử dụng danh sách hiểu & nbsp;

Giá trị không phải là một chuỗi trống trong Python và cả (không gian). in Python, and neither is (spaces).

Chuỗi trống có sai trong Python không?

Khoa học dữ liệu thực tế bằng cách sử dụng các chuỗi trống Python là "giả", điều đó có nghĩa là chúng được coi là sai trong bối cảnh boolean, vì vậy bạn chỉ có thể sử dụng không chuỗi.Empty strings are "falsy" which means they are considered false in a Boolean context, so you can just use not string.

Chuỗi trống có bằng không không?

Một chuỗi trống là một thể hiện chuỗi có độ dài bằng không, trong khi một chuỗi null không có giá trị nào cả.Một chuỗi trống được biểu diễn là "".Đó là một chuỗi ký tự của các ký tự bằng không.Một chuỗi null được biểu diễn bởi null., whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null .