2016-12-28 78 views
3

我想下面的Unicode字符串轉換到一個列表中列出清單結果[我扔在兩個空間,以使其適合...]:轉換CSV到列表

"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) 

這導致了這個雖然,我似乎無法刪除['','']容易...

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

你接近。有一些關於字符串支持在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'] 
+0

嗨,我正在嘗試此回覆。非常棒 - 謝謝! – keynesiancross

+0

關於你的編輯 - 我不知道我關注。如果x [0]]不起作用,原始細節是否= csvreader中x的[x [0])?它似乎對我來說 – keynesiancross

+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說你行作爲一個列表!

+0

如果某些元素包含逗號什麼?並直接傳遞'abc'沒有文檔/不支持。 –

+0

@ Jean-FrançoisFabre你的回答非常好。 :) – Alfabravo

+0

謝謝。我已經做了很長時間了。 –

-1

你有沒有試過string.split(',')?

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

+0

嗨,當我嘗試這個時,我得到:「58478000」只有 – keynesiancross

+0

我只能說我不(在Python 2和3中)。也許你在我的答案中留下了一些代碼。確保你有下面一行:'reader = csv.reader([abc],delimiter =',')'。 '[abc]'部分很重要。 – martineau

+0

你知道嗎,就是這樣。字符串周圍的額外[] – keynesiancross