0
嗨,我遇到了以下問題:寫作和排列讀出的內容爲CSV
我寫從列表/數組以下內容到一個CSV文件:
18 Aug 2015 12:00:08 AM CESTFrankfurtFRA:New Instruments available on XETRA - 1
18 Aug 2015 12:00:07 AM CESTFrankfurtFRA:Deletion of Instruments from XETRA - 1
18 Aug 2015 12:00:06 AM CESTFrankfurtFRA:New Instruments available on XETRA - 1
18 Aug 2015 12:00:05 AM CESTFrankfurtDIVIDEND/INTEREST INFORMATION -- 4`
用下面的函數
def _writeInCSV(text):
#print "Writing in CSV File"
with open('eggs.csv', 'wb') as csvfile:
#spamwriter = csv.writer(csvfile, delimiter='\t',quotechar='\n', quoting=csv.QUOTE_MINIMAL)
spamwriter = csv.writer(csvfile, delimiter='\t',quotechar="\n")
for n in range(len(text)):
spamwriter.writerow([text[n]])
現在我想從CSV文件中讀取內容到一個數組中,並且每個項目上總是顯示「[]」。因此,輸出如下所示:
["['18 Aug 2015 12:00:08 AM CESTFrankfurtFRA:New Instruments available on XETRA - 18.08.2015-002']", "['18 Aug 2015 12:00:07 AM CESTFrankfurtFRA:Deletion of Instruments from XETRA - 18.08.2015-001']"]
用於讀取CSV我使用下面的代碼/ funtion:
def _readInCSV(your_list):
with open('eggs.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
your_list.append(str(row))
的問題是,我試圖讓這些元素作爲字符串無「 []「開頭和結尾。
所以我想輸出是這樣的:
['18 Aug 2015 12:00:08 AM CESTFrankfurtFRA:New Instruments available on XETRA - 18.08.2015-002', '18 Aug 2015 12:00:07 AM CESTFrankfurtFRA:Deletion of Instruments from XETRA - 18.08.2015-001']
謝謝,現在完美的作品 – Nant
不要忘記接受答案,如果它幫助你。 –