[英]How to print csv from list and string combination without delimiter of space?
my csv file is created using values from a table as: 我的csv文件是使用表中的值创建的,如下所示:
with open(fullpath,'wb') as csvFile:
writer = csv.writer(csvFile,delimiter='|',
escapechar=' ',
quoting=csv.QUOTE_NONE)
string_values = 'abc|xyz|mno'
for obj in queryset.iterator():
writer.writerow([
smart_str(obj.code),#code
smart_str(string_values),# string of values
])
The csv generated should output result as: 生成的csv应将结果输出为:
code1|abc|xyz|mno
but what is generated is: 但是生成的是:
code1|abc |xyz |mno
I am unable to remove the space from the string while creating the csv. 创建csv时,无法从字符串中删除空格。 in whatever way may be.
无论如何。 The string values are generated dynamically and can have 1 or more string values.
字符串值是动态生成的,可以具有1个或多个字符串值。 I cannot use quoting=csv.QUOTE_NONE without specyfying escapechar=' ' ie a space.
我不能在没有特殊说明的情况下使用quoting = csv.QUOTE_NONE,即空格。 Please suggest.
请提出建议。
You can't just write strings containing the delimiter, unescaped! 您不能只写包含定界符且未转义的字符串!
Use, instead: 改为使用:
string_values = 'abc|xyz|mno'.split('|')
for obj in queryset.iterator():
writer.writerow([smart_str(obj.code)] + string_values)
IOW, writerow
a list of strings, and let the writer
insert the delimiters between the strings in the list! IOW,
writerow
一个字符串列表 ,然后让writer
在列表中的字符串之间插入定界符!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.