简体   繁体   English

如何在 csv 的单元格中列出列表中的每个项目?

[英]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.

相关问题 如何从python列表生成CSV文件,并将每个列表项放在单独的行中 - How to I generate a CSV file from a python list with each list item in a seperate row 如何为每个列表项制作带有列表和字典的 CSV 文件? - How to make a CSV file with a list and a dictionary for each list item? 如何将字符串列表写入 CSV 文件,列表中的每一项都在一个新行中? - How do I write a list of strings to a CSV file, with each item of the list on a new row? 如何将csv中的每一列保存到列表中? - How can I save each column in a csv into a list? 如何将搜索到的每个项目的链接存储到列表中? - How can I store the links found for each item searched into a list? 我如何通过选择然后删除每个项目来迭代 python 列表? - How can I iterate on python list by choosing and then removing each item? 如何在一行代码中打印每个列表项? - How can I print each list item, in a single line of code? 如何为该列表中的每个项目编号? 蟒蛇 - How can I number each item in this list? Python Python 3:列表和字典。 如何创建一个字典来告诉我每个项目来自哪个列表? - Python 3: lists and dictionary. How can I create a dictionary that tells me what list each item is from? 如何将列表中的值添加到 RDD 的每个项目中? - How do I add values from a list into each item of an RDD?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM