问题描述
我正在从CSV文件中读取一些数据,然后根据if语句打印一个值,但似乎没有意义。我希望它会打印等于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代码:
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
推荐答案
您的麻烦来自于您从文件总是一个字符串(即 str
type)。这意味着即使文件包含一个数字,它也会作为字符串读入您的变量。因此,如果您的文件如下所示:
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
如果您:
with open('myFile.txt') as infile:
x = infile.readline()
那么 x
会有值'2' code>,这是一个
str
类型。这意味着,如果你 x * 2
,你会得到 '22'
,因为这是字符串乘法
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.
你真正想要的是将该sting转换为 int
。这称为将字符串转换为整数,可以非常简单:
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)
还有一个你应该注意的类型: float
。它用于保存十进制数。所以,如果你说
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
那么 x
将是 float
。您还可以将 int
转换为 float
s:
then x
would be a float
. You can also cast int
s to float
s:
z = float(y)
会将 z
转换为浮点值, 2.0
您的实际问题:
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
这里,您正在尝试添加 str
和 int
。那么,这些是不兼容的类型添加。因此,您应该将 int
转换为 str
;就像 str
可以转换为 int
s, int
可以通过调用 str()
转换为 str
s:
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 str
s can be cast into int
s, int
s can be cast into str
s 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)
当然,print语句更友好一些, p>
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
这篇关于Python if语句使用CSV数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!