2014-04-01 76 views
0

我試圖寫入空的CSV文件。寫入CSV文件但格式不正確

我要上CSV文件的輸出是這樣的:

Date,Open,High,Low,Close,Volume,Adj Close 
2014-03-28,538.32,538.94,534.25,536.86,7106900,536.86 
2014-03-27,540.02,541.50,535.12,537.46,7929700,537.46 
2014-03-26,546.52,549.00,538.86,539.78,10706000,539.78 
2014-03-25,541.50,545.75,539.59,544.99,10081900,544.99 
2014-03-24,538.42,540.50,535.06,539.19,12703600,539.19 
2014-03-21,531.93,533.75,526.33,532.87,13373200,532.87 
2014-03-20,529.89,532.67,527.35,528.70,7442800,528.70 
2014-03-19,532.26,536.24,529.00,531.26,8027000,531.26 
2014-03-18,525.90,531.97,525.20,531.40,7487400,531.40 
2014-03-17,527.70,529.97,525.85,526.74,7126600,526.74 

但我最終得到一個非常大的行數,每一個字符是不同的小區。

我的代碼是:

with open('prova.csv','w',newline='') as f: 
    writer = csv.writer(f,delimiter=',',quotechar='"') 
    writer.writerows(request3) 
    f.flush() 
    f.close() 

和請求3的輸出是相同的表如上。

+0

請與編程語言添加一個標籤。 –

+0

它的Python,謝謝 – pkpkpk

+0

不知道它...... GL –

回答

2

如果request3是這樣的字符串:

request3 = """Date,Open,High,Low,Close,Volume,Adj Close 
2014-03-28,538.32,538.94,534.25,536.86,7106900,536.86 
2014-03-27,540.02,541.50,535.12,537.46,7929700,537.46 
2014-03-26,546.52,549.00,538.86,539.78,10706000,539.78 
2014-03-25,541.50,545.75,539.59,544.99,10081900,544.99 
2014-03-24,538.42,540.50,535.06,539.19,12703600,539.19 
2014-03-21,531.93,533.75,526.33,532.87,13373200,532.87 
2014-03-20,529.89,532.67,527.35,528.70,7442800,528.70 
2014-03-19,532.26,536.24,529.00,531.26,8027000,531.26 
2014-03-18,525.90,531.97,525.20,531.40,7487400,531.40 
2014-03-17,527.70,529.97,525.85,526.74,7126600,526.74""" 

然後是的,你將有問題。從the documentation

甲行必須是字符串或數字的用於編寫器對象序列中,並且字典映射字段名到字符串或數字(通過使它們通過STR()第一)爲DictWriter對象。

所以,如果你不使用DictWriter那麼你會希望有看起來像數據:

request3 = [['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close'], 
      ['2014-03-28', '538.32', '538.94', '432', '123'], 
      # more rows here 
      ] 
+0

如果'request3'是那個字符串,爲什麼不直接寫入文件呢?假設它應該有新行。 – 2rs2ts

+0

我已經將request3轉換爲列表,其中='日期','打開','高','低','關閉','音量','調節關閉\ n2014-03-28','538.32', '538.94','534.25','536.86','7106900','536.86 \ n2014-03-27','540.02','541.50',ecc通過執行** request3 = request3 [1:-1] .split (',')** – pkpkpk

+0

@Paolokiller注意你如何編寫一維列表。你需要一個2D列表。 – 2rs2ts

0

基於在評論您的意見,您在做以下幾點:

request3 = request3[1:-1].split(',') 

哪個給你買了1維列表:

['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close\n2014-03-28', 
'538.32', '538.94', '534.25', '536.86', '7106900', '536.86\n2014-03-27', 
'540.02', '541.50', ...] 

所以它看起來像你原來的request3像這樣的字符串:

"""Date,Open,High,Low,Close,Volume,Adj Close 
2014-03-28,538.32,538.94,534.25,536.86,7106900,536.86 
2014-03-27,520.02,541.50,...""" 

沒有好。你需要這樣的二維列表:

[['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close'], 
['2014-03-28', '538.32', '538.94', '534.25', '536.86', '7106900', '536.86'], 
['2014-03-27', '540.02', '541.50', ...]] 

幸運的是,不是很難解決。您需要先取得每一行,然後用逗號分隔每一行。雖然我顯示的多行字符串中沒有換行文字,但它們在那裏;你在輸出中看到了這個'Adj Close\n2014-03-28'。所以,先換行符!

request3Lines = request3[1:-1].split('\n') 
request3Separated = [line.split(',') for line in request3Lines] 

一個例子(忽略時使用的某些原因,片):

>>> request3 = """Date,Open,High,Low,Close,Volume,Adj Close 
2014-03-28,538.32,538.94,534.25,536.86,7106900,536.86 
2014-03-27,540.02,541.50,535.12,537.46,7929700,537.46""" 
>>> request3Lines = request3.split('\n') 
>>> request3Lines 
['Date,Open,High,Low,Close,Volume,Adj Close', '2014-03-28,538.32,538.94,534.25,536.86,7106900,536.86', '2014-03-27,540.02,541.50,535.12,537.46,7929700,537.46'] 
>>> request3Separated = [line.split(',') for line in request3Lines] 
>>> request3Separated 
[['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close'], ['2014-03-28', '538.32', '538.94', '534.25', '536.86', '7106900', '536.86'], ['2014-03-27', '540.02', '541.50', '535.12', '537.46', '7929700', '537.46']] 
+0

非常感謝,它的作品!我現在有我想要的格式的CSV文件。 – pkpkpk