-1

I am new to python and my teacher has told me to create a code that can edit a csv file by each field depending on the value in it. Here is a nested list to show the csv file which is split into lists by lines then by elements:

[["A","B","C","D"],["Yes",1,"05/11/2016","0"],["No","12","05/06/2017","1"],["Yes","6","08/09/2017","2"]]

What I am supposed to do is to make a loop which can be used to detect the postions of the elements within the inner list and then change the first element of each list to a "No" if it is a yes ,the 3rd element to today's date if the date stated is at least 6 months back and the last element to a 1 if it is more than 1 ,how am I supposed to do this?

Below is my code:

filename="Assignment_Data1.csv"
file=open(filepath+filename,"r")
reader=csv.reader(file,delimiter=",")
from datetime import datetime
six_months = str(datetime.date.today() - datetime.timedelta(6*365/12-1))
fm_six_months=str(datetime.datetime.strptime(six_months, '%Y-%m-%d').strftime('%d/%m/%Y'))

td=datetime.now()
deDate = str(td)[8:10] + "/"+ str(td)[5:7] + "/"+ str(td)[0:4]
import csv 
for row in reader:
    for field in row:
        if row[2]<=fm_six_months or row[4]>50 or row[2]<10:
            row[3]=deDate
            row[4]=0
            row[2]=100

Basically what I am trying to do is to replace the fields that have the above stated conditions with want I want through a loop ,is it possible?

nTIAO
  • 415
  • 1
  • 6
  • 15

1 Answers1

1

You're on the right track, but your code has a couple issues:

1) Import statements.

  • Your import statements should be all at the top of your program. Currently, you use csv.reader in line 3, but haven't imported csv yet.
  • The way you're importing of the datetime module is inconsistent with most of the code. This is somewhat confusing, since the datetime module also has a datetime class. Given what you want to do, it would be easiest to change the import statement to import datetime and change line 8 to td=datetime.datetime.now() (now is a function of the datetime class).

2) Iterating over field and row is redundant. The construct you have, for row in reader: for field in row, will run your if statements additional times that are unnecessary.

3) Python is zero-indexed. This means that the first element is a list is accessed using row[0], not row[1]. In your case, the fourth column of your CSV would be accessed with row[3].

4) You're combining conditions. From the phrasing of the assignment, it sounds like each of the conditions (like "change the first element to a No if it is a yes") is supposed to be independent of the other. However, if row[2]<=fm_six_months or row[4]>50 or row[2]<10 means that you'll change the data if any condition is true. It sounds like you need three separate if blocks.

5) Your code has no writer. This is really the big one. Simply saying row[2] = 100 doesn't do anything lasting, as row is just an object in memory; changing row doesn't actually change the CSV file on your computer. To actually modify the csv, you'll need to write it back out to file, using a csv.writer.

pbaranay
  • 585
  • 5
  • 17
  • ermm how would I be able to do this ? – nTIAO Aug 06 '17 at 12:27
  • I dont undestand how to do the writer – nTIAO Aug 06 '17 at 12:30
  • @nTIAO you need to put a little more effort into reading the answer, researching and testing. You can't expect every one to cater to your needs. – Sal Aug 06 '17 at 12:59
  • i did do research but my code is still not working should I upoload it here? – nTIAO Aug 06 '17 at 13:50
  • @nTIAO, yes, you should definitely edit your question with your most recent code. However, I'll also add that a good example of the CSV reader is on the page that I linked. – pbaranay Aug 06 '17 at 20:51