0

I have a csv file with four columns and about 26K rows. There are addresses in the fourth column and some of them have the country name at the end. I want to take the country string out of the file.

The current code I have is:

import csv


with open('file.csv', 'rt') as rf:
     reader = csv.reader(rf, delimiter=',')
     for row in reader:
         #print(row[3])    # this works
         usa = "United States"
         if usa in row[3]:
            #print("Its here!)")          #this works too
             repl = row[3].replace("United States", " " )
             # need to write to file- find out how to write changes to a csv file
with open('file.csv', 'w') as wf:
    writer = csv.writer(wf)
    writer.writerows(repl)

rf.close()
wf.close()

If you didn't already guess, it just deletes everything in the entire file.

My approach is obviously very wrong.

Do I pull each line into memory, check, replace where necessary, and re-write?

What is the best way to iterate through a CSV file (only editing a specific column, but searching all rows for that column?

Thanks!

Edit: This was marked as a duplicate, but my question is not how to replace a single value in a csv file- it is a question about how to find & replace a multitude of values, and what the appropriate algorithm looks like for traversing csv files. I have not found any existing SO questions that adequately address this.

Edit #2: As requested, the snippet looks like:

First Name,, Last Name,, ID,, Address
j-grimwood
  • 351
  • 3
  • 7
  • 19
  • 2
    What does your text file look like? Please provide a relevant snippet of the input with the expected output you want for that snippet. – ggorlen Aug 20 '19 at 19:14
  • 1
    Your code only replaces the last value. You need to open read and write in the same line and write every line . changed if needed, else unchanged into the new file. – Patrick Artner Aug 20 '19 at 19:16
  • 1
    Your replacing the country with a blank space then only writing that blank space to the new file. – Joe Aug 20 '19 at 19:16
  • 1
    close() is not needed when using contexthandler `with open(..., "r"):` – Patrick Artner Aug 20 '19 at 19:17
  • 1
    you can open 2 files in same context: `witch open("f1.txt","r") as r, open("f2.txt","w") as w:` – Patrick Artner Aug 20 '19 at 19:17
  • @PatrickArtner - The whole address is contained in row[3]. The country is only one part of the value in that row. Could I, perhaps: row[3].strip([usa]) ? – j-grimwood Aug 20 '19 at 19:36
  • 1
    `row[3] = row[3].replace("United States", " " )` - then write the row to your output file – Patrick Artner Aug 20 '19 at 19:37
  • @PatrickArtner To write to the csv, I passed row indexes to values row1 = row[0], row2 = row[1] ..., etc, And I named the row I messed with newrow. newrow = row[3].replace(usa, " ") This does work, but when I try to write... I use: writer.writerows(row1 + row2 + row3 + newrow) . Output still deletes almost everything in the file. Is there another method I could check out? – j-grimwood Aug 20 '19 at 19:50

0 Answers0