我在Python 2.7中运行了一个代码。我有三列ID1,ID2和DISTANCE。我希望距离的所有行值都小于10米。当我运行以下代码时,输出显示所有行。我希望距离小于10米。
import csv
# open and read the csv file into memory
file = open('C:\\Users\\ADMIN\\Desktop\\Data\\2 July\\ndata.csv')
reader = csv.reader(file)
# iterate through the lines and print them to stdout
# the csv module returns us a list of lists and we
# simply iterate through it
if __name__ == "__main__":
file=open("sourav.csv","w") ## open file in write mode
for row in reader:
if row[3]<='10':
print "({} {} {})".format(row[1], row[2], row[3])
file.write("{} {} {}\n".format(row[1], row[2], row[3]))
答案 0 :(得分:0)
您需要将距离转换为整数,然后才能针对值10进行检查:
而不是比较两个字符串:
if row[3]<='10':
您需要比较两个整数:
if int(row[3])<=10: