List.Writerow()以Excel CSV格式写入替代行

发布于 2025-02-04 06:19:39 字数 1040 浏览 1 评论 0原文

我正在尝试从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])

and the output is this:
enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声情话 2025-02-11 06:19:39

使用emacs或其他可以揭示详细文件内容的工具,
查找a.csv有什么样的线路终止器。

从双间距开始,我的猜测是它具有CRLF。
然后输入后,Excel将每个Cr(ASCII 13)解释为线结束,然后将随后的LF(ASCII 10)解释为也是 是一条线。
因此,您得到了两个,只有一个。
每条线结束时,只要LF就会变得更好。

docs 建议通过newline选项:

open('eggs.csv', 'w', 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:

open('eggs.csv', 'w', newline='')

cf the docs for open().


Carefully examine datee,
to verify it doesn't have extra whitespace or empty elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文