Python if statement using CSV Data

user2242044

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
inspectorG4dget

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python-xlsxwriter only writes as a text when using csv data

From Dev

Importing financial data into Python Pandas using read_csv

From Dev

Align column names with data in a CSV file using Python

From Dev

Using Python Queue with a "with" statement

From Dev

Tips on using the with statement in Python

From Dev

Using LOAD DATA INFILE statement when csv table headings don't match mySQL table headings?

From Dev

Using a conditional 'with' statement in Python

From Dev

Using Python to manipulate data from a CSV only applying it to the first result

From Dev

Python regex: using or statement

From Dev

how to push a csv data to mongodb using python

From Dev

copy data from csv to postgresql using python

From Dev

Extracting variable names and data from csv file using Python

From Dev

how to create 1 gb csv file with random data using python

From Dev

Using Python to group csv data

From Dev

how to write nested json data to CSV using python

From Dev

Extract column data from a CSV file using Python

From Dev

Importing data from CSV to MySQL using python

From Dev

Exporting data as CSV file from ServiceNow instance using Python

From Dev

Extract column data from a CSV file using Python

From Dev

Using Python Queue with a "with" statement

From Dev

How to check if an data already present in a CSV file using Python

From Dev

Using LOAD DATA INFILE statement when csv table headings don't match mySQL table headings?

From Dev

Save data to csv file using Python

From Dev

Python if statement using Modulo

From Dev

Selenium and Python using If statement

From Dev

CSV data into postgreSQL using Python

From Dev

Using Wildcard in CSV lookup with if/else statement in python

From Dev

Data structure for heterogeneous csv using python

From Dev

How to insert data into database using prepared statement query in Django and Python

Related Related

  1. 1

    Python-xlsxwriter only writes as a text when using csv data

  2. 2

    Importing financial data into Python Pandas using read_csv

  3. 3

    Align column names with data in a CSV file using Python

  4. 4

    Using Python Queue with a "with" statement

  5. 5

    Tips on using the with statement in Python

  6. 6

    Using LOAD DATA INFILE statement when csv table headings don't match mySQL table headings?

  7. 7

    Using a conditional 'with' statement in Python

  8. 8

    Using Python to manipulate data from a CSV only applying it to the first result

  9. 9

    Python regex: using or statement

  10. 10

    how to push a csv data to mongodb using python

  11. 11

    copy data from csv to postgresql using python

  12. 12

    Extracting variable names and data from csv file using Python

  13. 13

    how to create 1 gb csv file with random data using python

  14. 14

    Using Python to group csv data

  15. 15

    how to write nested json data to CSV using python

  16. 16

    Extract column data from a CSV file using Python

  17. 17

    Importing data from CSV to MySQL using python

  18. 18

    Exporting data as CSV file from ServiceNow instance using Python

  19. 19

    Extract column data from a CSV file using Python

  20. 20

    Using Python Queue with a "with" statement

  21. 21

    How to check if an data already present in a CSV file using Python

  22. 22

    Using LOAD DATA INFILE statement when csv table headings don't match mySQL table headings?

  23. 23

    Save data to csv file using Python

  24. 24

    Python if statement using Modulo

  25. 25

    Selenium and Python using If statement

  26. 26

    CSV data into postgreSQL using Python

  27. 27

    Using Wildcard in CSV lookup with if/else statement in python

  28. 28

    Data structure for heterogeneous csv using python

  29. 29

    How to insert data into database using prepared statement query in Django and Python

HotTag

Archive