1

I am reading in some data from a CSV file and then printing a value based on an if statement, but it doesn't seem to make sense to me. I would expect it would print equal to 1

PYTHON CODE:

import csv

#open CSV file
csvfile = open("C:\\python.csv", "rb")
data = csv.reader(csvfile)
data = [row for row in data]

#start loop through each item
for currentrow in range(1, 2):  # numbers off due to array starting at 0
    #grab one record data [row][col]
    Count = data[currentrow][7]

    print "Count equals: " + Count

    if Count > 1:
        print "greater than 1"
    if Count == 1:
        print 'equal to 1'

OUTPUT:

Count equals: 1.00
greater than 1
6
  • 1
    Try Count = int(float(data[currentrow][7])) inside your for loop Commented Aug 4, 2014 at 3:37
  • @inspectorG4dget you should probably post it as an answer. Looks logical. Commented Aug 4, 2014 at 3:38
  • 1
    Aside: if you're a beginner, I'd strongly recommend using Python 3. Not only does it have lots of improvements across the board, but you would have seen a very useful error message. Commented Aug 4, 2014 at 3:39
  • @alecxe: Checking to make sure that's really what OP wants. There's also the issue of range(1,2) that I don't fully understand. So it's hard to tell if what I suggested really fixes the problem in the general case Commented Aug 4, 2014 at 3:39
  • @inspectorG4dget this worked great, but now the line print "Count equals: " + Count doesn't work because it's combining a number and a string. Do I need to convert back to a string to print and if so how? Commented Aug 4, 2014 at 3:40

1 Answer 1

3

Your trouble stems from the fact that what you read from a file is always a string (i.e. str type). This means that even if the file contains a number, it is read into your variable as a string. Therefore, if your file looks like this:

myFile.txt:

2

And if you did:

with open('myFile.txt') as infile:
    x = infile.readline()

then, x would have the value '2', which is a str type. This means, that if you did x*2, you'd get '22', because that's how strings multiply out.

What you really want, is to convert that sting into an int. This is called "casting a string into an integer" and can be done very simply:

y = int(x)

There's another type that you should be aware of: float. It is used to hold decimal numbers. So, if you were to say

x = 3.4

then x would be a float. You can also cast ints to floats:

z = float(y)

would turn z into a float, with the value 2.0

Now, onto your actual problem:

data = [row for row in data]  # data is now a list of lists; each sublist is a list of strings
for currentrow in range(1,2):
    Count = data[currentrow][7]  # Count is now the string at index 7, of the sublist at index `currentrow`, i.e. 1.00
    Count = float(Count)  # Count is now the floating point value of the string '1.00'
    Count = int(Count)  # Count is now the integer value of 1.00, i.e. 1
    if Count > 1:
        print "greater than 1"
    if Count == 1:
        print "equal to 1"

Now, onto your second problem:

print 'Count equals ' + Count

Here, you are trying to add a str and an int. Well, those are incompatible types for addition. Therefore, you should cast the int into a str; and just like how strs can be cast into ints, ints can be cast into strs with a call to str():

Count_str = str(Count)

So when you want to print the value, you could do:

print "Count equals " + str(Count)

Of course, the print statement is a little more friendly and lets you do something like this:

print "Count equals", Count  # no casting needed here
Sign up to request clarification or add additional context in comments.

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.