我对 python 还很陌生,我一直在尝试找出一种方法来删除双引号并拆分 csv 文件中引号内的字段。
例如: 从 csv 文件导入的数据包含一行,其中包含以下内容:
data: 1 ; "2;3" ; "4;5" ; 6
我想要的期望输出是:
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)