Python 检查空白 CSV 值不起作用
我有一个 CSV 文件,我正在针对它运行一个脚本以插入到数据库中。 如果该值为空,那么我不想插入它。这是我的内容,
if attrs[attr] != '' and attrs[attr] != None:
log.info('Attriute ID: %s' % attr)
log.info('Attriute Value: %s' % attrs[attr])
sql = insert_attr_query(attrs[attr], object_id, attr)
cursor.execute(sql)
它是空白的,它不 = '' 或 None,那么它 =?
I have a CSV file and I am running a script against it to insert into a database.
If the value is blank then I don't want to insert it. Here is what I have
if attrs[attr] != '' and attrs[attr] != None:
log.info('Attriute ID: %s' % attr)
log.info('Attriute Value: %s' % attrs[attr])
sql = insert_attr_query(attrs[attr], object_id, attr)
cursor.execute(sql)
It's blank and it doesn't = '' or None, then wth does it =?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它可能是空格,即带有空格的制表符或字符串尝试:-
It's probably whitespace i.e. a tab or string with spaces try:-
据推测它包含空格。您可以通过打印
repr(attrs[attr])
来检查这一点,它将在其周围加上引号并在“\t”处显示选项卡 将代码更改为
if attrs[attr] 不是 None 并且attrs[attr].strip() !="":
Presumably it contains whitespace. You could check this by printing
repr(attrs[attr])
which will put quotes round it and show tabs at "\t"Change the code to
if attrs[attr] is not None and attrs[attr].strip() !="":
您应该(几乎)始终规范要插入数据库(或用于许多其他目的)的任何文本字符串中的空格。
规范化空白的方法是 (1) 去除任何前导空白 (2) 去除任何尾随空白 (3) 将任何内部空白(长度 >= 1)替换为 1 个空格 (U+0020)。
空格不应限于标准 Python 提供的内容,特别是如果您使用 Python 2.X 并且不使用 unicode 对象。例如,在默认的“C”语言环境中,“\xA0”不被视为空格,但它很可能表示无中断空格 (U+00A0)。
Python 2.X 的示例代码:
概括第二个函数:将每次出现的非标准空白字符替换为单个空格,然后进行拆分连接舞蹈。
You should (almost) always normalise whitespace in any text string that is intended for insertion in a database (or for many other purposes).
To normalise whitespace is to (1) strip any leading whitespace (2) strip any trailing whitespace (3) replace any internal runs (length >= 1) of whitespace by exactly 1 SPACE (U+0020).
Whitespace should not be limited to what standard Python provides, especially if you are working in Python 2.X and not using unicode objects. For example, in the default "C" locale, "\xA0" is not treated as whitespace but it's very likely to represent NO-BREAK SPACE (U+00A0).
Sample code for Python 2.X:
Generalizing the second function: replace each occurrence of a non-standard whitespace character by a single space and then do the split-join dance.