好的,我正在编写一个脚本,它通过.txt
文件进行解析,并根据在文件中找到的某些字符串,提出某些建议
假设我想通过parameters.txt
文件进行解析,该文件如下所示:
ztta/roll_memory = 250000
shm = 5000
现在,我使用的代码是:
a = 'ztta/roll_memory = 250000'
b = 'shm = 5000'
with open('parameter.txt', 'r') as myfile:
data = myfile.read()
if a in data:
print("the value of ztta/roll_memory is correct --> PASS")
else:
print("the value of ztta/roll memory is incorrect --> FAIL")
if b in data:
print("the value of shm is correct --> PASS")
else:
print("the value of shm is incorrect --> FAIL")
这里的问题是,如果.txt
文件中的某个参数的值如下所示:
ztta/roll_memory = 2500 (**instead of 250000**), the if statement would still be triggered.
谢谢
另一种放置
if a in data
的方法是:您可以看到,子字符串
a
包含在比较字符串中, 不管它后面有多少个零另一种说法是:
另一方面,您的代码是不可伸缩的。您应该尝试以
ints
而不是字符串的形式读入参数。即相关问题 更多 >
编程相关推荐