0

I need to be able to find and change a value in a CSV stock file. I know how to search for a value but when I try to use the csv.writer then it returns an error.

Traceback (most recent call last):
  File "\\pbsrvfile\ExamAccounts$\ITCA76\ochot_krystian_a453\Task 3\Task 3.pyw", line 8, in <module>
    for row in writer:
TypeError: '_csv.writer' object is not iterable

I have no idea why it is doing that. So far I have this for the code:

import csv
code=input('Enter code to search: ')
stock=open ('Stock Levels.csv', 'wb')
writer=csv.writer(stock)
for row in writer:
    for field in row:
        if field==code:
            print('Record found! \n')
            print('Code: '+row[0])
            print('Product name: '+row[1])
            print('Target level: '+row[2])
            target = int(row[2])
            print('Current level: '+row[3]+'\n')
            current = int(row[3])
            restock_amount = target - current
            restocked_value = restock_amount + current
            writer.writerow[3](restocked_value)
            print(row[1]+' has been restocked to '+restocked_value)

Can anyone tell me how to fix the error or if there is another way to modify the value. I am using python 3.2.3

6
  • If there is anything wrong with my question please just tell me. Commented Dec 1, 2016 at 11:31
  • 1
    I don't understand what you attempt to iterate over in the for row in writer line. Surely it can't be the empty CSV file you just opened, shouldn't it be some sort of input? Commented Dec 1, 2016 at 11:36
  • That part of the code is used for searching for the correct row Commented Dec 1, 2016 at 11:40
  • The file that I was trying to open was the file that had the data I wanted to modify. Commented Dec 8, 2016 at 10:51
  • Problem: opening a file with w instead of a truncates it to zero bytes. Commented Dec 8, 2016 at 12:46

1 Answer 1

2

Try this:

import csv
import os
code=input('Enter code to search: ')
with open ('Stock Levels.csv', 'r') as stock:
    reader=csv.reader(stock, delimiter=',')

    with open ('New Stock Levels.csv', 'w') as newStock:
        writer=csv.writer(newStock, delimiter=',')
        for row in reader:
            if code in row:
                print('Record found! \n')
                print('Code: '+row[0])
                print('Product name: '+row[1])
                print('Target level: '+row[2])
                target = int(row[2])
                print('Current level: '+row[3]+'\n')
                current = int(row[3])
                restock_amount = target - current
                restocked_value = restock_amount + current
                row[3] = restocked_value
                print(row[1]+' has been restocked to '+str(restocked_value))
            writer.writerow(row)
os.remove('Stock Levels.csv')
os.rename('New Stock Levels.csv', 'Stock Levels.csv')

If you use with open instead of open you don't have to close the file.

The code works like this:

  • Read stock levels (you might have to change the delimiter to whatever you have)
  • Look at each line in stock levels and modify it if you see your code to search
  • Write ALL lines of stock levels back to new stock levels, including the modified lines
  • Delete the old file to enable the New Stock Levels.csv file to be renamed to the older version (make sure you have a backup)
  • Rename New Stock Levels.csv to Stock Levels.csv

I don't think you can modify a file directly, I think you always have to read it, modify, write to temp file and then rename.

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

4 Comments

If the file is very big, you can also copy it to the temp file one line at a time.
I think it all works fine until the point where it tries to rename the new file. It returns an error that it cannot create a file when that file already exists.
your script must have permission to overwrite the file, perhaps it can only create files
The error was that python could not rename the new file while the old one existed, I found a solution to this and edited your answer to contain this information. Thanks for your help.

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.