[英]How can I list each item from list in a cell in csv?
I would like to add each item from a list in a cell of CSV, items are strings but when I export it, each letter of the string is in a different cell, instead of the complete string in a different cell.我想从 CSV 的单元格中的列表中添加每个项目,项目是字符串,但是当我导出它时,字符串的每个字母都在不同的单元格中,而不是完整的字符串在不同的单元格中。
This is my current code:这是我当前的代码:
import csv
list_names = ['1. Bar 61 Restaurant', '2. Companero', '3. 28 Church Row', '4. Miss Tapas', '5. The Little Taperia', '6. Twist', '7. Volare']
file_to_output = open("newfile.csv", "w", newline="")
csv_writer = csv.writer(file_to_output,delimiter=",")
for item in list_names:
csv_writer.writerows([item,])
file_to_output.close()
Instead of running the for loop do it directly.而不是直接运行 for 循环。
file_to_output = open("newfile.csv", "w", newline="")
csv_writer = csv.writer(file_to_output,delimiter=",")
csv_writer.writerows([list_names,])
file_to_output.close()
You can use the pandas module您可以使用 pandas 模块
import pandas as pd
list_names = ['1. Bar 61 Restaurant', '2. Companero', '3. 28 Church Row', '4. Miss Tapas', '5. The Little Taperia', '6. Twist', '7. Volare']
df = pd.DataFrame(list_name, columns=some_column_name)
df.to_csv("path", sep="\t")
Or use writerow:或使用 writerow:
import csv
list_names = ['1. Bar 61 Restaurant', '2. Companero', '3. 28 Church Row', '4. Miss Tapas', '5. The Little Taperia',
'6. Twist', '7. Volare']
file_to_output = open("newfile.csv", "w", newline="")
csv_writer = csv.writer(file_to_output, delimiter=",")
for item in list_names: csv_writer.writerow([item])
file_to_output.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.