Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.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文件,其输出如下: 14897,40.50891,-81.03926168.19999 但是CSV编写器会在开始和结束时使用引号继续编写输出 ‘14897,40.50891,-81.03926168.19999’ 当我正常打印行时,输出是正确的,但我需要执行line.split(),否则csv编写器会将输出设置为1,4,8,9,7等 但是当我执行line.split()时,输出是 ['14897,40.50891,-81.03926168.19999'] 写为“14897,4

我正在尝试写入CSV文件,其输出如下:

14897,40.50891,-81.03926168.19999

但是CSV编写器会在开始和结束时使用引号继续编写输出

‘14897,40.50891,-81.03926168.19999’

当我正常打印行时,输出是正确的,但我需要执行line.split(),否则csv编写器会将输出设置为1,4,8,9,7等

但是当我执行line.split()时,输出是

['14897,40.50891,-81.03926168.19999']

写为“14897,40.50891,-81.03926168.19999”

我怎样才能让这些引语消失?我已经尝试了csv.QUOTE\u无,但不起作用

with open(results_csv, 'wb') as out_file:
            writer = csv.writer(out_file, delimiter=',')
            writer.writerow(["time", "lat", "lon", "alt"])

            for f in file_directory):
                for line in open(f):
                    print line                        
                    line = line.split()
                    writer.writerow(line)
使用
line.split()
,您不是按照逗号进行拆分,而是按照空格(空格、换行符、制表符)进行拆分。因为没有,所以每行只有一个项目

由于此项包含逗号,
csv
模块必须引用以与实际分隔符(也是逗号)产生差异。您需要
line.strip().split(“,”
才能使其工作,但是

使用
csv
读取数据是解决此问题的更好方法:

替换为:

for line in open(some_file):
   print line                        
   line = line.split()
   writer.writerow(line)
作者:


您不需要手动读取文件。您可以简单地使用csv阅读器。 将内部for回路更换为:

# with ensures that the file handle is closed, after the execution of the code inside the block

with open(some_file) as file:
    row = csv.reader(file)  # read rows
    writer.writerows(row)   # write multiple rows at once

我通过反复试验找到了答案,甚至不知道它是如何工作的,但添加这两行行行就行了line=line.strip()line=line.split(“,”)是的,但是您最好使用csv来读取和写入文件。
# with ensures that the file handle is closed, after the execution of the code inside the block

with open(some_file) as file:
    row = csv.reader(file)  # read rows
    writer.writerows(row)   # write multiple rows at once