1

I have to use CSVs and make a list of people's contact detail, like emails , phone numbers and addresses.

I have a list of column names along the top: name, email, number, etc.

I need to write in a specific cell. User's can enter their name and then enter new information, like if they didn't have a phone number and now they do, they can enter it. I can find the row of a specific person as it starts with their name that I can search, but then I don't know how to write to the column of phone number.

My code is like this:

import csv

with open(csvfile.csv,a)as file:
    reader=cvs.reader(file)
    writer=csv.writer(file)
    for row in file:
       if row["First colunm"]==x:
           row[1]="still don't have a phone"
           writer.writerow(row)

The problem seems like it can't be both writing and reading at the same time, but i don't know what to do. I am using Python 3.

ayhan
  • 70,170
  • 20
  • 182
  • 203
  • Good hints in this post... https://stackoverflow.com/questions/4148772/overwriting-a-specific-row-in-a-csv-file-using-pythons-csv-module – dazedandconfused Nov 08 '17 at 19:32

1 Answers1

0

Cause your a student I am going to push you to figure out the total answer on your own. But the tool you will want to use is

pandas.iloc

This is an integer based finding and it could be as simple as

df.iloc[0,1] = whatever you need it to be.

Hopefully this gets you a step closer :)

Best,

Andy

EDIT Realized your just Using CSV

If you can, I would recommend loading your dataframe through Pandas to work with CSV. Its an overall more powerful tool that packs in alot of what you will need to solve this issues.

If you want I can help you set up the pandas but see this for the answer regarding CSV module

Writing to a particular cell using csv module in python

Sorry for my mistake in not reading as fully,

Best,

Andy

A. McMaster
  • 303
  • 1
  • 10