[英]Double quotes keep coming in csv when merging file in python, how to remove?
我有許多帶有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)
現在看來這行之有效,需要一行來刪除不必要的,添加的雙引號(隱藏且僅在每個url的末尾)。
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)])
您需要將csv.QUOTE_NONE
作為參數傳遞給csv.QUOTE_NONE
,而不是分配給單獨的變量:
writeFile = csv.writer(fi, quoting=csv.QUOTE_NONE)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.