mx.church_
mx.church_

Reputation: 17

Not finding variable within CSV File

So here i am looking for the name "Steve" in my csv file. However it cant find it? (It prints "Not found" to shell indicating that it ran the else statement)

This is my CSV File:

Dave    55
Steve   3
Max     56

This is my python File:

import csv
name = "Steve"
score = "5"
classname = "Class A"

if classname == "Class A":
    f = open('Class A.csv', 'r')
    csvread = csv.reader(f)
    for row in csvread:
        if row[0] == name:
            with open('Class A.csv', 'a', newline='') as fp:
                print("FOUND")
        else:
            with open('Class A.csv', 'a', newline='') as fp:
                print("NOT FOUND")

(My code extends beyond this but this is the issue bit so I'l save you the whole program)

Upvotes: 0

Views: 143

Answers (3)

krethika
krethika

Reputation: 4476

Since your file seems to be separated by whitespace instead of a character delimiter, you cannot use csv.reader to parse it.

instead of

csvread = csv.reader(f)
for row in csvread:

try this

for line in f:
    row = line.split()

this should give the result you are looking for.

Upvotes: 0

Maxime Biette
Maxime Biette

Reputation: 303

Your file doesn't seem to be properly formatted as a CSV.

The most common CSV formatting that you can see is with commas (could be semicolon or have quotes depending on internationalization):

Dave,55
Steve,3
Max,56

The code to read this CSV would be the following indeed:

import csv
f = open('comma_separated.csv', 'r')
csvread = csv.reader(f)
for row in csvread:
    if row[0] == "Steve":
        print("FOUND")
    else:
        print("NOT FOUND")

In your "csv" I see no seperator. It looks like a fixed width formatting (meaning it is not a CSV file), meaning the value of each column will always take the same space. You can then split your data just by selecting the column where the data are. Another way could be to use regular expression.

f = open('fixed_width.txt', 'r')
for row in f:
    if row[0:5].strip() == "Steve":
        print("FOUND")
    else:
        print("NOT FOUND")

As other suggested, if the fields are indeed separated with tabulation the file should look that way (If you copy-paste in a text editor you will have a better chance at seeing the tabulation between the values):

Dave    55
Steve   3
Max 56

The corresponding code will get you the answer you need with tabulations:

import csv
f = open('tab_separated.tsv', 'r')
csvread = csv.reader(f, delimiter='\t')
for row in csvread:
    if row[0] == "Steve":
        print("FOUND")
    else:
        print("NOT FOUND")

Also please note that in my examples that I removed the open() of the file in append mode because I don't see the use case of doing so and it was not answering your question.

Upvotes: 1

Leb
Leb

Reputation: 15953

It's still a little vague on your expected outcome, but you definitely to change print("FOUND") to fp.write("whatever the output is")

The way you're opening the file and appending to it at the same time, can be dangerous and not recommended. It's always better to write to a new file to prevent issues.

If you're wanting to write Steve to the new file then just row[0] if you're wanting to write Steve 3 then you need to split the list of ("Steve", 3) before writing. write() only allows strings, not a list or tuple.

Let me know if this clarifies things.

Upvotes: 0

Related Questions