Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-从元组CSV文件中删除大括号和逗号_Python_Csv - Fatal编程技术网

Python-从元组CSV文件中删除大括号和逗号

Python-从元组CSV文件中删除大括号和逗号,python,csv,Python,Csv,我正在打印到csv文件,如下所示: bdictionary = ## bdictionary is a big list of tuples w = csv.writer(open("testnucsv.csv", "w")) for sent in bdictionary: w.writerow(sent) 它打印得非常精细,看起来像这样: bdictionary = ## bdictionary is a big list of tuples w = csv.writer(open

我正在打印到csv文件,如下所示:

bdictionary = ## bdictionary is a big list of tuples
w = csv.writer(open("testnucsv.csv", "w"))
for sent in bdictionary:
    w.writerow(sent)
它打印得非常精细,看起来像这样:

bdictionary = ## bdictionary is a big list of tuples
w = csv.writer(open("testnucsv.csv", "w"))
for sent in bdictionary:
    w.writerow(sent)
(你的,你的)狗的,你的)。。。。。。。。。。。。。。。 (u'The',u'D')

我怎么能这样打印出来

我的狗 D

这是我尝试过的,但它不起作用。它分割了每个字符:

w = csv.writer(open("testnucsv.csv", "w"))
for sent in bdictionary:
    sent = ''.join(str(v) for v in sent)
    w.writerow(sent)

将其包装在列表中,writerow需要一个iterable,因此它会在字符串上迭代,将其拆分为单个字符:

sent = [' '.join(" ".join(v) for v in sent)]
您还需要如上所述连接元组中的字符串,而不是在元组上调用str,即:

t = [(u'My', u'D'), (u'dog', u'N')]

print(" ".join([" ".join(v) for v in t]))
My D dog N
您也可以只使用file.write并将连接的字符串传递给它:

with open("testnucsv.csv", "w") as f:
   for sent in bdictionary:
        f.write(" ".join([" ".join(v) for v in sent])+"\n")

最后一段代码不是为我编译的。无效语法谢谢,它在我使用的第一个代码块中工作,但我检查了你的第三个代码块,我想让你知道它没有为我编译。谢谢@不用担心,不用客气。谢谢你让我知道