【发布时间】:2014-11-22 19:27:24
【问题描述】:
我找不到更好的地方来问我的问题。我正在学习 Python 并尝试创建如下脚本。
1) 应该可以搜索 csv 文件。
2) 如果找到匹配,则返回整行。
我的 csv:
Product,Scan,Width,Height,Capacity
LR,2999,76,100,17.5
RT,2938,37,87,13.4
如果我搜索 2938 为例,整行返回如下:
Product: RT
Scan: 2938
Width: 37
Height: 87
Capacity: 13,4
到目前为止我有:
csvFile = getComponent().filePath
pos = csvFile.rfind('Desktop\\')
csvFile = csvFile[:pos] + 'programm\\products.csv'
myfile = open(csvFile)
myfile.seek(0)
for line in myfile.split('\n'):
data = line.split(',')
print data
if data[2] == agv_O_Cal.value and data[3] == agv_O_Mod.value:
print 'found: value = %s' %agv_O_Cal.value, agv_O_Mod.value
Product = data[5]
Scan = data[6]
Width = data[7]
Height = data[9]
Capacity = data[10]
print , Product, Scan, Width, Height, Capacity
解决方案不起作用。
【问题讨论】:
-
你当前的输出是什么?
-
缩进错误:意外缩进
-
刚刚添加了答案,如果没有意义,请在此处评论
-
我编辑你的代码。试试看。
标签: python csv search return row