List.Writerow()以Excel CSV格式写入替代行
我正在尝试从Yahoo导入数据,并将其写入与下载的格式不同的CSV文件中,但是每当我这样做时,它都会将数据打印到Excel中的每个替代单元格中。我很困惑,因为我找不到其他人。我写的代码是:
with open(file, "w") as f:
writer = csv.writer(f, quoting=csv.QUOTE_ALL)
writer.writerow(fields)
for word in datee:
writer.writerow([word])
Timestamp Open High Low Close Adj Close Volume Split
02-06-2022
01-06-2022
31-05-2022
27-05-2022
26-05-2022
25-05-2022
24-05-2022
有人可以帮我理解为什么吗?
I'm trying to import data from Yahoo and write it into a csv file in a different format to what I download, but whenever i do this, it prints the data into every alternate cell in excel. I'm confused as i can't find anyone else who had this. My code for the write is this:
with open(file, "w") as f:
writer = csv.writer(f, quoting=csv.QUOTE_ALL)
writer.writerow(fields)
for word in datee:
writer.writerow([word])
Timestamp Open High Low Close Adj Close Volume Split
02-06-2022
01-06-2022
31-05-2022
27-05-2022
26-05-2022
25-05-2022
24-05-2022
Can anyone help me understand why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用emacs或其他可以揭示详细文件内容的工具,
查找a.csv有什么样的线路终止器。
从双间距开始,我的猜测是它具有CRLF。
然后输入后,Excel将每个Cr(ASCII 13)解释为线结束,然后将随后的LF(ASCII 10)解释为也是 是一条线。
因此,您得到了两个,只有一个。
每条线结束时,只要LF就会变得更好。
docs 建议通过
newline
选项:cf docs for
open> open open(Open) /代码>。
仔细检查
日期
,为了验证它没有额外的空白或空元素。
Use emacs, or another tool that can reveal detailed file contents,
to find what kind of line terminators ATHE.csv has.
From the double spacing, my guess is it has CRLFs.
And then upon input, Excel interprets each CR (ascii 13) as end-of-line, and the subsequent LF (ascii 10) as also being an end-of-line.
So you get two, where just one was intended.
You'd be better off with just LF at the end of each line.
The docs suggest passing a
newline
option:cf the docs for
open()
.Carefully examine
datee
,to verify it doesn't have extra whitespace or empty elements.