简体   繁体   English

如何使用python将列表或字符串写入特定列的csv文件?

[英]How I can write a list or string to a specific column csv file by using python?

How I can write a list or string to a specific column csv file by using python? 如何使用python将列表或字符串写入特定列的csv文件?

txt_file = path+"allfils"
csv_file = path+"mycsv.csv"
in_txt = open(txt_file, "r")
out_csv = open(csv_file, 'w')
file_string = in_txt.read()
file_list = file_string.split(',')
writer = csv.writer(out_csv)
writer = csv.DictWriter(out_csv,['Script Name', 'description',extrasaction='ignore')

writer.writeheader()

for row in file_list:
    out_csv.writelines(row) 

so under 'Script Name' rows should have only script names and then in 'description' column I want to put only descriptions. 因此,在“脚本名称”下,行应仅包含脚本名称,然后在“描述”列中,我仅要放置描述。

description is a list of multiple descriptions if I do this, I get descriptions as rows under same column "Script name' under every script description是一个多个描述的列表,如果执行此操作,则每个脚本下的“ Script name”列均以行形式显示

Script Name 
a.tcl
description1
description2
b.tcl
description1
description2

but I want to see script name and description in their columns as below 但我想在他们的栏中看到脚本名称和描述,如下所示

Script Name                   Description
a.tcl                         description1, description2
b.tcl                         description1, description2

您需要确保在csv_out.writerow时将其传递给一个包含2个元素的数组,其中第一个元素是脚本名称,第二个第二个元素是所有说明的单个字符串。

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

相关问题 如何使用 python 将列表写入另一个 csv 文件的列 - How to write a list into column of another csv file using python 如何使用Python将字符串写入csv文件中的列 - How to write a string to a column in csv file using Python 如何在 Python 中的 csv 文件的特定列中测试条件 - How can I test a conditional in a specific column of a csv file in Python 如何使用 Python 从 csv 文件中标记特定列中的所有行? - How can i tokenize all rows in a specific column from a csv file using Python? 如何使用Python编写大型的csv文件? - How can I write a large csv file using Python? 如何使用Python将.txt格式的字符串列出为.csv格式? - How can I list a string in .txt to .csv format using Python? 如何将字典中的现有 csv 文件写入特定列? - How can I write to an existing csv file from a dictionary to a specific column? 如何查询我已输入的csv文件的特定列并使用python打印所有返回的行? - How can i query a specific column of a csv file that i have input and print all returned rows using python? 我如何使用另一个 CSV 名称列表搜索 CSV 并返回是否使用 python 找到特定单词 - How can i search a CSV using another CSV list of names and return if it find a specific word using python 如何在python的csv文件中写入字符串列表? - how do write a list of string into a csv file in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM