0
import csv

with open("C:\\Users\\ki386179\\Desktop\\output.csv","r") as f:
    reader = csv.reader(f)
    for row in reader:
        if 'india' in row:
            pass
        if 'india' not in row:
            with open("C:\\Users\\ki386179\\Desktop\\output.csv","w") as f:
                writer = csv.writer(f)
                writer.writerow('india')

I am trying to achieve something like this , to check for a particular value in a particular column and if not write to it.

5
  • 1
    I'm guessing your problem may be you have 2 variables f. Also you're trying to write to file you have open Commented Aug 23, 2020 at 11:21
  • "to achieve something like this" - please provide what exactly you expect Commented Aug 23, 2020 at 11:22
  • Hi @JanStránský Thanks for your response , I have a string to check in the first column of a csv file, if it is there , i want to do nothing if it is not then i want to write at the end of the first column. all using csv modlue .. , Commented Aug 23, 2020 at 11:31
  • 1
    What I meant by exactly is that you edit your question, providing sample initial state of the output.csv file and expected final state of the file Commented Aug 23, 2020 at 11:34
  • Code which doesn't do what you want is a poor way of explaining what you do want anyway. Commented Aug 23, 2020 at 11:51

2 Answers 2

1

You can't write to a file whilst you are in the middle of reading from it. Also, surely you don't want to require every line to contain the string, like your current logic does?

I'm guessing you want to add one line at the end if none of the lines matched:

import csv

seen_string = False

with open("C:\\Users\\ki386179\\Desktop\\output.csv","r") as f:
    reader = csv.reader(f)
    for row in reader:
        if 'india' in row:
            seen_string = True
            break

if not seen_string:
    # Notice append mode; "a" not "w"
    with open("C:\\Users\\ki386179\\Desktop\\output.csv","a") as f1:
        # Noice this one is f1
        writer = csv.writer(f1)
        writer.writerow('india')

Maybe notice also that in looks for a substring; if the file contains e.g. amerindian" the code will regard that as a match. Check for string equality with == instead if that's what you want.

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

Comments

1

You can use r+ mode for both reading and writing. Although it is difficult to understand what you are trying to achieve here, an example code that can do what you're probably trying to achieve is shown below. I put the above code in a script india.py and made it executable (chmod +x india.py)

#!/usr/bin/env python

import sys
from csv import reader, writer

with open(sys.argv[1], "r+") as f:
    for row in reader(f):
        if "india" in row:
            break
    else:
        writer(f).writerow("india")

A test run

$ cat test_write.csv
us,canada,mexico
france,germany,norway
brazil,argentina,colombia
china,japan,korea
$ ./india.py test_write.csv
$ cat test_write.csv
us,canada,mexico
france,germany,norway
brazil,argentina,colombia
china,japan,korea
i,n,d,i,a

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.