将CSV转换为列表会导致列表中的列表

时间:2016-12-28 18:54:37

标签: python csv

我正在尝试将以下Unicode字符串转换为列表[我扔了两个空格以使其适合...]:

child_process

我使用以下代码将上面的字符串"58478000","","NEW","2016-12-28T14:34:18","C","","","N","N","ON","2017-03-15","2022-03-15","30/360","EUR","IR","","InterestRate:IRSwap:FixedFloat","Trade","EUR-EURIBOR-Reuters","FIXED","Percent","-0.003","","","EUR","EUR","25,000,000","25,000,000","3M","1Y","3M","","","","","","","","","","","","",""转换为列表:

abc

这导致了这一点,我似乎无法轻易放弃csv.reader(abc, delimiter=',') details = list(csvreader) ......

['','']

我的目标是将其放入某种列表或任何其他集合中,以便更容易从中提取信息......

5 个答案:

答案 0 :(得分:2)

获得CSV行后,您只需使用

即可
result = abc.split(",") 

要使列表避免使用逗号作为元素(您的方法是使用引号作为字符串分隔符,似乎。

此外,使用csv库本身,this question可能有所帮助。

>>> import csv

>>> with open(name, 'rb') as f:
...     for row in csv.reader(abc, delimiter=',', skipinitialspace=True):
...             result = row

那里的答案说引号应该自动处理,而this answer表示你将行作为列表!

答案 1 :(得分:2)

你很亲密。 documentation中有关于字符串支持的内容:

  

虽然模块不直接支持解析字符串,但可以轻松完成:

import csv
for row in csv.reader(['one,two,three']):
    print(row)

注意字符串周围的[]。如果没有这些大括号,csv模块似乎每个元素返回1行,但这似乎没有文档。

由于我最初没有从文档中了解上述部分,我的第一个"实用主义"方法是使用列表理解来获取行内的元素:

import csv

abc = '"58478000","","NEW","2016-12-28T14:34:18","C","","","N","N","ON","2017-03-15","2022-03-15","30/360","EUR","IR","","InterestRate:IRSwap:FixedFloat","Trade","EUR-EURIBOR-Reuters","FIXED","Percent","-0.003","","","EUR","EUR","25,000,000","25,000,000","3M","1Y","3M","","","","","","","","","","","","",""'

csvreader=csv.reader(abc, delimiter=',')
details = [x[0] for x in csvreader]
print(details)

的产率:

['58478000', '', '', '', 'NEW', '', '2016-12-28T14:34:18', '', 'C', '', '', '', '', '', 'N', '', 'N', '', 'ON', '', '2017-03-15', '', '2022-03-15', '', '30/360', '', 'EUR', '', 'IR', '', '', '', 'InterestRate:IRSwap:FixedFloat', '', 'Trade', '', 'EUR-EURIBOR-Reuters', '', 'FIXED', '', 'Percent', '', '-0.003', '', '', '', '', '', 'EUR', '', 'EUR', '', '25,000,000', '', '25,000,000', '', '3M', '', '1Y', '', '3M', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']

通过这种方法,我们去除空字符串(这只是一种解决方法),如果我们完整地需要它们,我们就会卡住BTW:

details = [x[0] for x in csvreader if x[0]]

结果:

['58478000', 'NEW', '2016-12-28T14:34:18', 'C', 'N', 'N', 'ON', '2017-03-15', '2022-03-15', '30/360', 'EUR', 'IR', 'InterestRate:IRSwap:FixedFloat', 'Trade', 'EUR-EURIBOR-Reuters', 'FIXED', 'Percent', '-0.003', 'EUR', 'EUR', '25,000,000', '25,000,000', '3M', '1Y', '3M']

但在阅读完文档后,正确的方法是:

import csv

abc = '"58478000","","NEW","2016-12-28T14:34:18","C","","","N","N","ON","2017-03-15","2022-03-15","30/360","EUR","IR","","InterestRate:IRSwap:FixedFloat","Trade","EUR-EURIBOR-Reuters","FIXED","Percent","-0.003","","","EUR","EUR","25,000,000","25,000,000","3M","1Y","3M","","","","","","","","","","","","",""'

csvreader=csv.reader([abc], delimiter=',') # pass a list of 1 element
details = list(csvreader)[0]
print(details)

结果(请注意,现在遵循空字符串的数量):

['58478000', '', 'NEW', '2016-12-28T14:34:18', 'C', '', '', 'N', 'N', 'ON', '2017-03-15', '2022-03-15', '30/360', 'EUR', 'IR', '', 'InterestRate:IRSwap:FixedFloat', 'Trade', 'EUR-EURIBOR-Reuters', 'FIXED', 'Percent', '-0.003', '', '', 'EUR', 'EUR', '25,000,000', '25,000,000', '3M', '1Y', '3M', '', '', '', '', '', '', '', '', '', '', '', '', '']

如果我们要删除空字符串,请使用列表解析来执行此操作,比其他字符串更简单:

csvreader=csv.reader([abc], delimiter=',')
details = [x for x in list(csvreader)[0] if x]
print(details)

结果:

['58478000', 'NEW', '2016-12-28T14:34:18', 'C', 'N', 'N', 'ON', '2017-03-15', '2022-03-15', '30/360', 'EUR', 'IR', 'InterestRate:IRSwap:FixedFloat', 'Trade', 'EUR-EURIBOR-Reuters', 'FIXED', 'Percent', '-0.003', 'EUR', 'EUR', '25,000,000', '25,000,000', '3M', '1Y', '3M']

答案 2 :(得分:2)

由于您只有一个字符串,因此您需要将其放在可迭代中,例如list,以便csv.reader能够正确读取它。

import csv

abc = (u'"58478000","","NEW","2016-12-28T14:34:18","C","","","N","N","ON",'
       u'"2017-03-15","2022-03-15","30/360","EUR","IR","",'
       u'"InterestRate:IRSwap:FixedFloat","Trade","EUR-EURIBOR-Reuters","FIXED",'
       u'"Percent","-0.003","","","EUR","EUR","25,000,000","25,000,000","3M",'
       u'"1Y","3M","","","","","","","","","","","","",""')

reader = csv.reader([abc], delimiter=',')
details = next(reader)  # read and process single line that is in list [abc]
print(details)

输出:

['58478000', '', 'NEW', '2016-12-28T14:34:18', 'C', '', '', 'N', 'N', 'ON', '2017-03-15', '2022-03-15', '30/360', 'EUR', 'IR', '', 'InterestRate:IRSwap:FixedFloat', 'Trade', 'EUR-EURIBOR-Reuters', 'FIXED', 'Percent', '-0.003', '', '', 'EUR', 'EUR', '25,000,000', '25,000,000', '3M', '1Y', '3M', '', '', '', '', '', '', '', '', '', '', '', '', '']

答案 3 :(得分:1)

如果第一个输入是一个字符串,则可以使用ast.literal_eval()

>>> import ast
>>> 
>>> s = '"58478000","","NEW","2016-12-28T14:34:18","C","",""'
>>> list(ast.literal_eval(s))
['58478000', '', 'NEW', '2016-12-28T14:34:18', 'C', '', '']

如果要展平结果,可以使用:

>>> import itertools
>>> 
>>> my_list = [['58478000'], ['', ''], [''], ['', ''], ['NEW'], ['', ''], ['2016-12-28T14:34:18'], ['', ''], ['C'], ['', ''], ['']]
>>>
>>> list(itertools.chain.from_iterable(my_list))
['58478000', '', '', '', '', '', 'NEW', '', '', '2016-12-28T14:34:18', '', '', 'C', '', '', '']

答案 4 :(得分:-1)

你试过string.split(',')吗?