我正在做的是为我的python脚本提供一个CSV文件,其中包含由逗号分隔的数百万条记录。任何字符串都是“双qoutes”。
我通过我的python脚本传递这个.csv文件
import csv
import string
import sys, getopt
inFile = open(sys.argv[1], 'r')
outFile = open(sys.argv[1][:-4] + '_no-nulls.csv', 'w')
data = csv.reader(inFile)
writer = csv.writer(outFile)
specials = "NULL"
for line in data:
line = [value.replace(specials, '') for value in line]
writer.writerow(line)
inFile.close()
outFile.close()
最终结果是我的所有引号都没有 我做错了什么?
示例输入:
897555,2021-03-31 00:00:00.000,NULL,"45687","B","QA",29,NULL,NULL,NULL,NULL,NULL,NULL,NULL,"5648987QEXXX",6,NULL,NULL,"DOE","JOHN",NULL,NULL,NULL,NULL,NULL,"Q",1994-04-24 00:00:00.000,"R","CX","ZZ",NULL,NULL,NULL,NULL,NULL,"Y",NULL,"GA","R","DE",NULL,NULL,NULL,NULL,NULL,"EN",NULL,"Y","OP",NULL,"R","XZ",NULL,NULL,NULL,"8945564",2005-03-01 12:00:00.000,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
示例输出:
897555,2021-03-31 00:00:00.000,,"45687","B","QA",29,,,,,,,,"5648987QEXXX",6,,,"DOE","JOHN",,,,,,"Q",1994-04-24 00:00:00.000,"R","CX","ZZ",,,,,,"Y",,"GA","R","DE",,,,,,"EN",,"Y","OP",,"R","XZ",,,,"8945564",2005-03-01 12:00:00.000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
答案 0 :(得分:1)
这很正常。在阅读时,def view_task(request):
if request.method == 'GET':
context = dict()
context['task'] = Task.objects.all()
return render(request, 'todo/view_tasks.html', context)
if request.method == 'POST':
task_name = request.post['task_name']
Task.objects.delete(task_name)
Task.objects.save()
将删除引号,因为它假定使用数据的程序不想要或不需要它们。如果有必要,csv.reader
会重新启用它们,具体取决于您传递的quoting
的设置,默认为csv.writer
- 如果字符串中有字符,则只会添加引号可能被误解了。
您可以将阅读器和编写器都设置为QUOTE_MINIMAL
以保留原始文件中的引号,或将编写器设置为QUOTE_NONE
以重新引用所有输出。