gpt4 book ai didi

python - 将 CSV 转换为列表导致列表中的列表

转载 作者:太空宇宙 更新时间:2023-11-03 13:35:04 26 4
gpt4 key购买 nike

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

"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","固定","百分比","-0.003","","","EUR","EUR","25,000,000","25,000,000","3M","1Y","3M""","","","","","","","","","","",""",""

我使用以下代码将上面的字符串 abc 转换为列表:

csv.reader(abc, delimiter=',')
details = list(csvreader)

虽然结果是这样,但我似乎无法轻易删除 ['','']...

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

我的目标是将其放入某种列表或任何其他集合中,这样可以更轻松地从中提取信息...

最佳答案

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

And while the module doesn’t directly support parsing strings, it can easily be done:

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', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']

使用这种方法,我们必须去除空字符串(这只是一种解决方法),如果我们需要它们完整无缺,顺便说一句,我们陷入困境:

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']

关于python - 将 CSV 转换为列表导致列表中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41368078/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com