我正在尝试为CSV文件创建一个简单的解析器。我正在尝试使用最简单的方法通过解析器查找值(行)
import csv
"""
with open('C:/MAIL07072021180029.csv', 'r') as file:
proxy = csv.reader(file)
for row in proxy:
print(row[0])
"""
identifyingnumber = input("What is the number? ")
with open('C:/MAIL07072021180029.csv', 'r') as file:
data = csv.reader(file)
for row in data:
if row[1] == (identifyingnumber):
print(row)
运行代码并输入代理编号(识别编号,excel中的数据)后。节目刚刚停止?这不是打印行
以下是我的csv文件中的示例数据:
Card Identifying Sequence
1873356
我打印出了第0行,如果该行打印成功,则打印出识别号
这是某种逻辑错误吗?我想不出来
您的csv文件中可能没有标识号,在这种情况下,由于您的“如果”语句,将不会打印任何内容。您可以检查是否找到了该号码,如果找到了,请中断(如果不想检查其他行中该号码的另一个实例),如果没有,请打印您从未找到该号码
我强烈推荐熊猫,因为你正在尝试做的事情。在Pandas中打开csv,然后立即检查所有行的值,而不是遍历行https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html
编辑以将我们在评论中讨论的所有内容放在一起:
相关问题 更多 >
编程相关推荐