嗨,我有一些问题。
mpv_set_option_string(mpv, "input-default-bindings", "no");
'names.csv'结果
acol bcol ccol
a a a
acol bcol ccol
b b b
acol bcol ccol
c c c
acol bcol ccol
d d d
acol bcol ccol
e e e''
有whiteline和colume重复 我想要没有白行且没有colume repeat的结果
答案 0 :(得分:3)
你可以试试这个
import csv
test = ['a','b','c','d','e']
with open('names.csv', 'wb') as csvfile:
fieldnames = ['acol', 'bcol', 'ccol']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for t in test:
writer.writerow({'acol': t, 'bcol': t, 'ccol': t})
答案 1 :(得分:1)
import csv
test = ['a','b','c','d','e']
for i,t in enumerate(test):
with open('names.csv', 'a') as csvfile:
fieldnames = ['acol', 'bcol', 'ccol']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
if i == 0:
writer.writeheader()
writer.writerow({'acol': t, 'bcol': t, 'ccol': t})
Divakar提供的另一个解决方案更加优雅和快捷,因为您不会在每个循环中打开文件。
答案 2 :(得分:1)
使用此
import csv
test = ['a','b','c','d','e']
for t in test:
with open('names.csv', 'w') as csvfile:
fieldnames = ['acol', 'bcol', 'ccol']
writer = csv.writer(csvfile)
writer.writerow(fieldnames)
for elem in test:
writer.writerow(elem * 3)
这是names.csv
的输出,它没有任何换行符
acol,bcol,ccol
a,a,a
b,b,b
c,c,c
d,d,d
e,e,e
答案 3 :(得分:0)
import csv
test = ['a','b','c','d','e']
with open('names.csv', 'a') as csvfile:
fieldnames = ['acol', 'bcol', 'ccol']
writer = csv.DictWriter(csvfile,delimiter=' ',fieldnames=fieldnames,lineterminator=' ')
writer.writeheader()
for t in test:
writer.writerow({'acol': t, 'bcol': t, 'ccol': t})
您正在循环中编写标题,因此它们会被重写多次,默认情况下行终止符是' \ n'它给出了行尾的新空格。这给出了上面引用的结果