写入CSV文件但格式错误

时间:2014-04-01 16:28:23

标签: python file 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的输出与上面相同。

2 个答案:

答案 0 :(得分: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

  

对于DictWriter对象,行必须是Writer对象的字符串或数字序列,以及将字段映射到字符串或数字(通过首先传递给str()的字典)。

因此,如果您没有使用DictWriter,那么您会希望获得如下数据:

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

答案 1 :(得分:0)

根据您在评论中的反馈,您正在执行以下操作:

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

它为您提供了一维列表:

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