• I.D.: AN000015544
DESCRIPTION: 6 1/2 DIGIT DIGITAL MULTIMETER
MANUFACTURER: HEWLETT-PACKARDMODEL NUM.: 34401A CALIBRATION - DUE DATE:6/1/2016 SERIAL NUMBER: MY45027398
• I.D.: AN000016955
DESCRIPTION: TEMPERATURE CALIBRATOR
MANUFACTURER: FLUKE MODEL NUM.: 724 CALIBRATION - DUE DATE:6/1/2016 SERIAL NUMBER: 1189063
• I.D.: AN000017259
DESCRIPTION: TRUE RMS MULTIMETER
MANUFACTURER: AGILENT MODEL NUM.: U1253A CALIBRATION - DUE DATE:6/1/2016 SERIAL NUMBER: MY49420076
读取文本文件并将每个部分的ID号和序列号保存到part_data数据结构中。
part_data = {'ID': [],
'Serial Number': []}
with open("textfile", 'r') as part_info:
lineArray = part_info.read().split('\n')
print(lineArray)
if "• I.D.: AN000015544 " in lineArray:
print("I have found the first line")
ID = [s for s in lineArray if "AN" in s]
print(ID[0])
我的代码没有找到I.D:或序列号值。我知道这是错误的我试图使用我从这个网站Text File Reading and Printing Data获得的方法来解析数据。任何人都可以让我朝着正确的方向收集价值吗?
这个解决方案适用于python 2.7.9而不是3.4,多亏了多米诺骨牌 - https://stackoverflow.com/users/209361/domino:
with open("textfile", 'r') as part_info:
lineArray = part_info.readlines()
for line in lineArray:
if "I.D.:" in line:
ID = line.split(':')[1]
print ID.strip()
然而,当我最初问我使用python 3.4的问题时,解决方案无法正常工作。
有谁理解为什么它不能工作python 3.4?谢谢!
答案 0 :(得分:1)
这应该打印出你的所有ID。我认为它应该让你朝着正确的方向前进。
with open("textfile", 'r') as part_info:
lineArray = part_info.readlines()
for line in lineArray:
if "I.D.:" in line:
ID = line.split(':')[1]
print ID.strip()
答案 1 :(得分:1)
它在python3中不起作用,因为在python3中打印是一个函数
它应该结束
print(ID.strip())