I am trying to remove from some list elements some extra double quotes by using strip or replace but they are not removed. I am using the following
with open(source_initial2, 'r', encoding='ISO-8859-1',newline='') as csvf:
reader = csv.reader(csvf, delimiter=";")
for row in reader:
for i in range(len(row)):
if "\"" in row[i]:
row[i].replace('\"','')
igecontents.append(row)
return igecontents
The row in csv is like the following:
DCA.P/C.06190;7613329007877;WENGER, COBALT 16 COMPUTER BACKPACK;" BLUE (R)""";35,92;Yes
DCA.P/C.06191;7613329007914;WENGER, PILLAR 16 COMPUTER BACKPACK;" BLACK/GRAY (R)""";35,92;No
This works fine:
import csv
with open('names.csv', 'r') as csvf:
output = []
for row in csvf.readlines():
output.append(row.replace('\"', ''))
with open('outfile','a') as f:
f.writelines(output)
COntent of outfile:
DCA.P/C.06190;7613329007877;WENGER, COBALT 16 COMPUTER BACKPACK; BLUE (R);35,92;Yes
DCA.P/C.06191;7613329007914;WENGER, PILLAR 16 COMPUTER BACKPACK; BLACK/GRAY (R);35,92;No
You have some extras bits in your code that are unecessary - like checking if the quotes are in the line or not.... Also no need for a reader object - you've already opened the file with the with open(...") as csvf statement.
python中str对象的replace
函数返回一个新的str,并且不修改调用它的对象,因此您应该做的是
row[i] = row[i].replace('\"','')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.