如何使用python检查csv文件中的每个值大于或小于零?
我想检查一列中的每个值,并根据这些值在下一列中给它们添加标签(趋势)。例如,如果该值大于零或等于或小于零,则根据该值,将在下一列中写入正、负和相同的标签 我的输入文件如下所示:如何使用python检查csv文件中的每个值大于或小于零?,csv,python-3.4,Csv,Python 3.4,我想检查一列中的每个值,并根据这些值在下一列中给它们添加标签(趋势)。例如,如果该值大于零或等于或小于零,则根据该值,将在下一列中写入正、负和相同的标签 我的输入文件如下所示: Weightage /// column name 0.000555 0.002333 0 -0.22222 And I want my output file is look like: Weightage Labels // column name
Weightage /// column name
0.000555
0.002333
0
-0.22222
And I want my output file is look like:
Weightage Labels // column name
0.000555 positive
0.002333 positive
0 same
-0.22222 negative
有人能帮我吗
代码是:
print (results)
for r in results:
if r >0:
print("test")
label = "positive"
print(label)
elif r == 0.0:
label = "equal"
print(label)
else:
print("nothing")
我在“r”for循环中遇到问题。
出现以下错误:
Traceback (most recent call last):
File "C:\Python34\col.py", line 23, in <module>
if r >0:
TypeError: unorderable types: tuple() > int()
回溯(最近一次呼叫最后一次):
文件“C:\Python34\col.py”,第23行,在
如果r>0:
TypeError:无序类型:tuple()>int()
乍一看,您似乎混淆了行和列。我建议使用更明确的名称。这有助于避免混淆。另外,不要将字符串与整数等数字类型进行比较。它将在Python2中给出令人惊讶的结果。在Python3中,这是一个错误
for row in results:
column = row[0] # The first column of this row.
value = float(column) # The csv module returns strings, so we should
# turn them into floats for numeric comparison.
if value > 0:
print "positive"
elif value < 0:
print "negative"
else:
print "zero"
对于结果中的行:
column=行[0]#此行的第一列。
value=float(column)#csv模块返回字符串,因此我们应该
#将它们转换为浮点数进行数值比较。
如果值>0:
打印“正片”
elif值<0:
打印“负片”
其他:
打印“零”
您似乎将其与代码编写服务混淆了。您的尝试在哪里?具体问题是什么?在结果中为r打印(结果):如果r>0:print(“test”)label=“positive”print(label)elif r==0.0:label=“equal”print(label)否则:在此代码中打印(“nothing”),我只检查条件并希望在shell上输出。编辑问题,阅读后我没有足够的权限编辑问题…单击显示“编辑”的位置?或者:如果这回答了你的问题,请考虑接受答案。谢谢:)