My last question was considered a duplicate, but I haven't found a question remotely similar to what I am asking, so I will rephrase:
I have a csv file, four columns, and about 26,000 rows.
The data is as follows for every row:
Firstname,, Lastname,, ID,, Address
In the last column, the address column, the addresses are formatted as follows:
1234 Streetname Dr.
Timbuktu, AK 32456
United States
My goal is only to remove the country name, from every row that contains it (not all rows do), preserving the rest of the address, and write this back to the file. I want all the other data to remain as it was. Basically: any instance of...say... the substring "United States" and replace it with a blank space.
The code I presently have is as follows:
import csv
with open('file.csv', 'rt') as rf:
reader = csv.reader(rf, delimiter=',')
for row in reader:
#print(row[3] + "\n") # this works
usa = "United States"
row1 = row[0]
row2 = row[1]
row3 = row[2]
if usa in row[3]:
newrow = row[3].replace(usa, " ")
#print(newrow + "\n")
with open('file.csv', 'w') as wf:
writer = csv.writer(wf)
writer.writerows(row1 + row2 + row3 + newrow)
It is presently deleting the CSV file nearly clean. Some strange single chars are left over in a few rows, only in the first column.
Can someone help point me to the snag? Thanks.