更新时间:2023-02-18 16:11:38
正如注释中所述,在处理CSV文件时,您确实应该使用Python的内置 csv模块(似乎链接到Python 2文档
As noted in the comments, when dealing with CSV files you truly ought to use Python's built-in csv module (linking to Python 2 docs since it seems that's what you're using).
另外要注意的是,当您这样做时:
Another thing to note is that when you do:
data = line.split(csvDelimiter)
返回列表中的每个项目,如果不为空,则为字符串。在循环中进行类型检查没有任何意义(尽管如果有原因,您会使用 isinstance
)。尽管您可能使用了unicode字符串,但我不知道对此有什么解决办法。在Python 2上,通常可以使用 isinstance(...,basestring)
,其中 basestring
是两个 str
和 unicode
。在Python 3上,只需使用 str
,除非您知道要处理的是 bytes
。
every item in the returned list, if it is not empty, will be strings. There's no sense in doing a type check in the loop (though if there were a reason to you would use isinstance
). I don't know what "didn't work" about it, though it's possible you were using unicode strings. On Python 2 you can usually use isinstance(..., basestring)
where basestring
is a base class for both str
and unicode
. On Python 3 just use str
unless you know you're dealing with bytes
.