[英]Python: write variables' values in CSV file
I'm trying to write some variables (sql query values) into a csv file, delimited by "|"我正在尝试将一些变量(sql 查询值)写入 csv 文件,以“|”分隔as below:如下:
with open(f'/c/doc/output/testfile.csv', 'w') as outcsv:
writer = csv.writer(outcsv)
cursor.execute(sqlscript)
for row in cursor:
p_date = row['t_date']
p_order = row['t_order']
results = ("|".join([f"{p_date}"+ f"{p_order}"]))
writer.writerow(results)
but the results are splitted by ',': 2,0,2,0,-,0,4,-,0,3,A,A,0,0,1但结果被','分割:2,0,2,0,-,0,4,-,0,3,A,A,0,0,1
any suggestions?有什么建议么?
You can use the delimiter
optional parameter to specify the new delimiter.您可以使用delimiter
可选参数来指定新的分隔符。 The default delimiter
is comma so you have to specify the new delimiter as |
默认delimiter
是逗号,因此您必须将新分隔符指定为|
. .
Use:利用:
with open(f'your_csv_file_path', 'w') as outcsv:
writer = csv.writer(outcsv, delimiter="|")
cursor.execute(sqlscript)
for row in cursor:
p_date = row['t_date']
p_order = row['t_order']
writer.writerow([p_date, p_order])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.