elementid = 123456 # given
searchTerm = str(elementid)
with open('wawi.txt', 'r') as f:
for index, line in enumerate(f, start=1):
if searchTerm in line:
print('elementid exists on line #{}'.format(index))
numbers = re.findall(r'\d+', line) # extract all numbers
numbers = [int(x) for x in numbers] # map the numbers to int
found = elementid in numbers # check if exists
将整数转换为字符串,然后迭代文件中的行,检查当前行是否与
elementid
匹配。在输出
^{pr2}$另一种方法
一个更可靠的解决方案是从每一行中提取所有的数字,并在所述数字中找到数字。如果当前行中的任何位置都存在该数字,则将声明匹配。在
方法
示例
^{4}$当您
read
文件时,您将整个内容读入一个字符串。在当你迭代它时,你一次只得到一个字符。在
尝试打印线条:
你会得到
^{pr2}$你需要说:
只是重新排列你的代码
相关问题 更多 >
编程相关推荐