我有这个清单:
comment = ['Item Location: AUMEL222\nLintu Testi: C:\\SSD\\important\\text.txt\nLintu Connection: 123\nItem Version: ABC.123.ABC\nItem Serial: 1234ABCD\nItem Configuration ID: ABCD1234']
我需要从这里提取某些项目。我已经让它工作了,但必须有更简单的方法来做到这一点。我的代码如下所示:
key = "Item Location"
key_found = False
for line in comment:
if key_found:
get_value(line) #converts the big list to more readable state.
line2 = line
teststat = ""
FW = ""
print(line2)
for item in line2.split("\n"):
if "Item Location" in item:
Teststat = (item.strip())
if "Item Version" in item:
FW = (item.strip())
print(Teststat)
print(FW)
输出:
Item Location : AUMEL222
Item Version : ABC.123.ABC
所以从一个字符串中获取一些想要的值。主要目标是只打印出价值。不是关键。但这可以通过以下方式完成:
print(Teststat.replace("Item Location: ", ""))