简体   繁体   English

如何逐行写一个csv文件?

[英]How to write a csv file line by line?

I am trying to scrape data from a website and I have collected 3 different type of information from the website. 我正在尝试从网站上抓取数据,并且已经从网站上收集了3种不同类型的信息。 I have thousands of records in the 3 list but for simplicity, I am adding a few records. 我在3个列表中有成千上万的记录,但为简单起见,我添加了一些记录。

List1 = ['DealerName']
List2 = ['Person1','Person2']
List3 = ['crisp nori, hot rice, and cold fish','takeout,fancy presentation, piled']

I have to write an output csv file line by line with 3 columns(List1, List2, List3) and the list information for the 3 columns. 我必须逐行编写一个输出csv文件,其中包含3列(List1,List2,List3)和3列的列表信息。 The 'DealerName' is constant for all records. 'DealerName'对于所有记录都是恒定的。 I am facing trouble because there are commas in List3 which is separating the information in individual columns(different cells). 我遇到了麻烦,因为List3中的逗号分隔了各个列(不同单元格)中的信息。 The desired output file should look like this 所需的输出文件应如下所示

项目清单

Thanks for the comments. 感谢您的评论。 Based on one of the comments, I made some modifications in the code and tried using the following code but it's not giving me the desired output. 根据其中一项评论,我对代码进行了一些修改,并尝试使用以下代码,但未提供所需的输出。

List1 = ['DealerName']
List2 = ['Person1','Person2']
List3 = ['crisp nori, hot rice, and cold fish','takeout,fancy presentation, piled']

Output_File = open("Output.csv", "w")
Output_File.write("List1,List2,List3")

import csv, itertools
rows = itertools.zip_longest([List1, List2, List3])
c = csv.writer(Output_File)
c.writerows(rows)

Output_File.close()

Use csv: 使用csv:

import csv, itertools
rows = itertools.zip_longest(List1, List2, List3)
csvwriter.writerows(rows)

The csv module will automatically wrap strings containing comma in a quote, which will read fine. csv模块会自动将包含逗号的字符串括在引号中,这样会很好看。

Edit: 编辑:

You can loop over the rows and output them with writerow instead of writerows, and that would fulfill your requirement of doing this line by line. 您可以遍历行并使用writerow而不是writerows将其输出,这将满足您逐行执行此操作的要求。

Edit 2: I've fixed my answer. 编辑2:我已经确定答案。 itertools.zip_longest([List1, List2, List3]) should be itertools.zip_longest(List1, List2, List3) itertools.zip_longest([List1, List2, List3])应该是itertools.zip_longest(List1, List2, List3)

Also you're going to want a newline after your header so Output_File.write("List1,List2,List3\\n") instead of Output_File.write("List1,List2,List3") 另外,您将需要在标题后添加换行符,以便使用Output_File.write("List1,List2,List3\\n")而不是Output_File.write("List1,List2,List3")

Use pandas: 使用熊猫:

import pandas as pd
df = pd.DataFrame([List1, List2, List3]).T 
df.to_csv('your.csv', index=False)

Pandas will automatically wrap strings containing comma in a quote, which will read fine. 熊猫会自动将包含逗号的字符串括在引号中,这样会很好看。

In this particular case (in other words, not in the most general sense), specifying the first element of List1 as the fillvalue argument when calling itertools.zip_longest() looks like it would make it work: 在这种特殊情况下(换句话说,不是最一般的意义),在调用itertools.zip_longest()时将List1的第一个元素指定为fillvalue参数似乎可以使它工作:

import csv, itertools

List1 = ['DealerName']
List2 = ['Person1','Person2']
List3 = ['crisp nori, hot rice, and cold fish', 'takeout,fancy presentation, piled']

with open("Output.csv", "w", newline="") as Output_File:
    Output_File.write("List1,List2,List3\n")
    writer = csv.writer(Output_File)
    rows = itertools.zip_longest(List1, List2, List3, fillvalue=List1[0])
    writer.writerows(rows)

Contents of output.csv file afterward: 之后的output.csv文件的内容:

List1,List2,List3
DealerName,Person1,"crisp nori, hot rice, and cold fish"
DealerName,Person2,"takeout,fancy presentation, piled"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM