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)    

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.