[英]How to remove double quotes from a csv file
我對 python 還是很陌生,我一直在嘗試找出一種方法來刪除雙引號並從 csv 文件中拆分引號內的字段。
例如:從 csv 文件中導入的數據由一行組成:
data: 1 ; "2;3" ; "4;5" ; 6
我想要的 output 將是:
output: 1 , 2 , 3 , 4 , 5 , 6
我嘗試使用以下代碼來實現此目的,盡管它無法將引號中的分號識別為分隔符:
with open('file.csv','r') as csv_file:
csv_reader = csv.reader(csv_file,delimiter=';',quoting=csv.QUOTE_NONE,skipinitialspace=True, escapechar='"' )
next(csv_reader)
output = ""
for line in csv_reader:
output += line[0] + ',' + line[1]+ ',' + line[2] + ',' + line[3] + ',' + line[4] + ',' + line[5]
print(output)
import csv
with open('file.csv','r') as csv_file:
csv_reader = csv.reader(csv_file,delimiter=';',quoting=csv.QUOTE_NONE, skipinitialspace=True)
output = ""
# iterate over every line of the csv
for line in csv_reader:
# iterate over each element of the line
for i in range(len(line)):
line[i] = line[i].strip(" ").strip("\"")
output += ", ".join(line) + "\n" # remove the "\n" if you want no return character
print(output)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.