1

每个人。

我想将 csv 解析"20568078","240431","jpg","St, Carolina 1920",,["20568078","240431","jpg","St, Carolina 1920","",""]

通过使用这个:

string=r'"20568078","240431","jpg","St, Carolina 1920",,'
reader = csv.reader(string)
for r in reader:
    print (r)

我得到了输出:

['20568078']
['', '']
['240431']
['', '']
['jpg']
['', '']
['St, Carolina 1920']
['', '']
['', '']

我想知道如何删除不必要的['', ''],并将最后两个更改,,""and""

谢谢你。

4

2 回答 2

4

用于StringIO将此刺痛视为类似文件的对象

>>> import csv
>>> from StringIO import StringIO
>>> string=r'"20568078","240431","jpg","St, Carolina 1920",,'
>>> for r in csv.reader(StringIO(string)):
        print (r)


['20568078', '240431', 'jpg', 'St, Carolina 1920', '', '']

http://docs.python.org/2/library/csv.html#csv.reader

csv.reader(csvfile, dialect='excel', **fmtparams)

返回一个读取器对象,它将遍历给定的行csvfilecsvfile可以是任何支持迭代器协议并在每次next()调用其方法时返回一个字符串的对象——file对象和list对象都适用。

您的代码的问题是您将单个字符串传递给reader. 然后读者将每个字符解释为一行。例如。

>>> string=r'"20568078","240431","jpg","St, Carolina 1920",,'
>>> i = iter(string)
>>> next(i)
'"'
>>> next(i)
'2'

大约是您传递给它的可迭代对象的csv调用方式。next但是,您使用表示多行字符串的双引号,这解释了为什么它们都是一体的。

于 2013-05-14T06:45:21.340 回答
2

您可以将字符串列表传递给csv.reader构造函数:

>>> import csv
>>> string=r'"20568078","240431","jpg","St, Carolina 1920",,'
>>> reader = csv.reader([string])
>>> print reader.next()
['20568078', '240431', 'jpg', 'St, Carolina 1920', '', '']
于 2013-05-14T06:55:11.297 回答