我有很多带URL链接的文件,我想将它们全部合并到一个大文件中。个人文件中的链接没有双引号。合并文件以某种方式为最终csv(MergedURLs.csv)中的每个链接添加了双引号。我读了python的csv模块文档,并添加了行" writeFile = csv.QUOTE_NONE",但没有区别。
import csv
def mergeFile(a, b, x, y):
for loop1 in range(a, b):
for loop2 in range(x, y):
try:
fileName1 = "FoundValidURLs_"
fileName2 = "_"
fileName3 = ".csv"
fileNameComplete = fileName1 + str(loop1) + fileName2 + str(loop2) + fileName3
with open(fileNameComplete, "rb") as f:
for URLrecords in f:
with open("MergedURLs.csv", "ab") as fi:
writeFile = csv.writer(fi)
writerFile = csv.QUOTE_NONE
writeFile.writerow([URLrecords])
except IOError:
continue
loop2 += 1
loop1 += 1
mergeFile(1, 2, 1, 3)
答案 0 :(得分:2)
这似乎现在正在运行,需要一行来删除不必要的,添加的双引号(隐藏且仅在每个网址的末尾)。
with open(fileNameComplete, "rb") as f:
for URLrecords in f:
with open("MergedURLs.csv", "ab") as fi:
writeFile = csv.writer(fi)
URLrecords_strip = URLrecords[0:-1] # strip away the quotation at the end
writeFile.writerow([str(URLrecords_strip)])
答案 1 :(得分:1)
您需要将csv.QUOTE_NONE
作为参数传递给作者,而不是分配给单独的变量:
writeFile = csv.writer(fi, quoting=csv.QUOTE_NONE)