简体   繁体   中英

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?

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

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个元素的数组,其中第一个元素是脚本名称,第二个第二个元素是所有说明的单个字符串。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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