Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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_Python 3.x_Csv - Fatal编程技术网

Python 从csv行中删除双引号

Python 从csv行中删除双引号,python,python-3.x,csv,Python,Python 3.x,Csv,我有两个文件:src.csv和dst.csv。下面的代码从src.csv读取第二行,并将其附加到dst.csv。问题是dst.csv中的输出包含在双引号(“”)中 预期结果: 10, 5, 5, 10, 1 输出: "10, 5, 5, 10, 1" 我尝试在csv.writer中使用quoting=csv.QUOTE\u NONE,escapechar='',它确实删除了引号,尽管输出现在在每个csv值后都包含一个空格 这是我的密码: import csv with open('src.

我有两个文件:src.csv和dst.csv。下面的代码从src.csv读取第二行,并将其附加到dst.csv。问题是dst.csv中的输出包含在双引号(“”)中

预期结果:

10, 5, 5, 10, 1
输出:

"10, 5, 5, 10, 1"
我尝试在csv.writer中使用
quoting=csv.QUOTE\u NONE,escapechar=''
,它确实删除了引号,尽管输出现在在每个csv值后都包含一个空格

这是我的密码:

import csv

with open('src.csv', 'r') as src, open('dst.csv', 'a', newline='') as dst:
    wr = csv.writer(dst, dialect='excel', delimiter=',', quoting=csv.QUOTE_NONE, escapechar=' ')
    next(src)
    for row in src:
        wr.writerow([row.rstrip('\n')])

有什么建议吗?

我认为您必须使用
csv.reader()
将行作为数字列表来读取-现在您将行作为一个字符串来读取,csv.writer必须添加
,因为您在这个字符串中有

您没有将源文件行拆分为列,所以您只编写了一列csv。改用读卡器:

import csv

with open('src.csv', 'r') as src, open('dst.csv', 'a', newline='') as dst:
    wr = csv.writer(dst, dialect='excel', delimiter=',', quoting=csv.QUOTE_NONE, escapechar=' ')
    next(src)
    reader = csv.reader(src)
    for row in reader:
        wr.writerow(row)

只要使用
writerow(row)
。改变它就会得到:
1,0,5,5,1,0,1,
你一定没有按我的意思做。见@tdelaney's。