2

I have a python script that appends 4 strings to the end of my csv file. The first column is the user's email address, and I want to search the csv to see if that users email address is already in the file, if it is I want to overwrite that whole row with my 4 new strings, but if not I want to continue to just append it to the end. I have it searching the first column for the email, and if it is there it will give me the row.

with open('Mycsvfile.csv', 'rb') as f:
  reader = csv.reader(f)
  indexLoop = []
  for i, row in enumerate(reader):
      if userEmail in row[0]:
          indexLoop.append(i)
f.close()
with open("Mycsvfile.csv", 'ab') as file222:
    writer = csv.writer(file222, delimiter=',')
    lines = (userEmail, userDate, userPayment, userStatus)
    writer.writerow(lines)
file222.close()

I want to do something like this, if email is in row it will give me the row index and I can use that to overwrite the whole row with my new data. If it isn't there I will just append the file at the bottom. Example:

with open('Mycsvfile.csv', 'rb') as f:
  reader = csv.reader(f)
  new_rows = []
  indexLoop = []
  for i, row in enumerate(reader):
      if userEmail in row[0]:
          indexLoop.append(i)
          new_row = row + indexLoop(userEmail, userDate, userPayment, userStatus)
          new_rows.append(new_row)

      else: 
          print "userEmail doesn't exist"
          #(i'd insert my standard append statement here.

f.close
#now open csv file and writerows(new_row)    
Ecom1414
  • 79
  • 1
  • 7

2 Answers2

1

For this, you're better off using Pandas, rather than the csv module. That way you can read the whole file into memory, modify it, and then write it back to a file.
Be aware though that, modify DataFrames in place is slow, so if you have a lot of data to add, you're better of transforming it in into a dictionary and back.

import pandas as pd

file_path = r"/Users/tob/email.csv"
columns = ["email", "foo", "bar", "baz"]
df = pd.read_csv(file_path, header=None, names=columns, index_col="email")
data = df.to_dict('index')

for email, foo, bar, baz in information:
    row = {"foo": foo, "bar": bar, "baz"}
    data[email] = row

df = pd.DataFrame(data)
df.to_csv(file_path)

Where information is whatever your script returned.

Batman
  • 8,571
  • 7
  • 41
  • 80
0

First you don't need to call the close function when using with, python does it for you.

If you have the index you can do:

with open("myFile.csv", "r+") as f:
    # gives you a list of the lines
    contents = f.readlines()
    # delete the old line and insert the new one
    contents.pop(index)
    contents.insert(index, value)
    # join all lines and write it back
    contents = "".join(contents)
    f.write(contents)

But I would recommand you to do all the operations in one function because it doesn't make a lot of sense to open the file, iterate on its lines, close it, reopen it and updating it.

Rémi Rigal
  • 134
  • 5