2

Require the program to search for a row then adjust a particular element of the CSV before re-saving.

Any help appreciated!

import csv
import os
username=input('Enter username: ')
with open ('user.csv', 'r') as user:
    reader=csv.reader(user, delimiter=',')
    print(reader)

with open ('newuser.csv', 'w') as newdetail:
    writer=csv.writer(newdetail, delimiter=',')
    for colomn in reader:
        if username in coloum[1]:
            print('User found! \n')
            print('UserID: '+colomn[0])
            print('Username: '+colomn[1])
            print('Interest: '+colomn[2])
            change = (column[2])
            change = input("What is your new interest?")
            print(colomn[2]+' has been changed to ', change)
        writer.writerow(colomn)
os.remove('user.csv')
os.rename('newuser.csv', 'user.csv')

The current error is:

dir/ line 10, in <module>
    for colomn in reader:
ValueError: I/O operation on closed file.

Much appreciated, this is where I am up to now. Currently only prints the single line that's being edited and not all of the other movies that have not been adjusted.

import csv
import os
new = []
username=input('Enter movie: ')
with open ('movies.csv', 'r') as user:
    reader=csv.reader(user, delimiter=',')
    #print(reader)

    with open ('newmovies.csv', 'w') as newdetail:
        writer=csv.writer(newdetail, delimiter=',')
        for colomn in reader:
            if username in colomn[1]:
                print('Movie found!')
                print('MovieID: '+colomn[0])
                print('Movie name: '+colomn[1])
                print('Interest: '+colomn[2])
        choice = input("what is your new interest?")
        print('Interest has been changed to ', choice)
        new = [(colomn[0]),(colomn[1]),(choice)]
        writer.writerow(new)
os.remove('movies.csv')
os.rename('newmovies.csv', 'movies.csv')
2
  • Why on earth did you introduce errors in the code given by Dimgold? The answer is still in his answer and still caused by indentation errors. Commented Jul 6, 2017 at 13:45
  • The current solution does change the data where required although all of the other content is deleted which is frustrating. Commented Jul 7, 2017 at 11:11

1 Answer 1

1

reader object is not containing the data, but only has a direct access to the file. Once you close the user.csv file (when exiting from with scope) the reader is no more useful.

Try to put the second with inside the 1st scope:

with open ('user.csv', 'r') as user:
    reader=csv.reader(user, delimiter=',')
    print(reader)
    with open ('newuser.csv', 'w') as newdetail:
        writer=csv.writer(newdetail, delimiter=',')
        for colomn in reader:
            if username in colomn[1]:
                print('User found! \n')
                print('UserID: '+colomn[0])
                print('Username: '+colomn[1])
                print('Interest: '+colomn[2])
                change = (column[2])
                change = input("What is your new interest?")
                print(colomn[2]+' has been changed to ', change)
            writer.writerow(colomn)

P.S. - fix the 11th line as well - colomn instead of coloum

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

1 Comment

Much appreciated, this is where I am up to now. Currently only prints the single line that's being edited and not all of the other movies that have not been adjusted.

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.