文件中的字符串格式相同,node:9000006 id:0x0001 proxy:0x0000 info:0x40
要求把每行:后的字符串提取出来,变成9000006 0x0001 0x0000 0x40存储在变量中。- import os
- file_path = os.path.abspath(r'C:\Users\14232\Downloads\topo.txt')
- #打开文件
- print("文件路径:",file_path)
- with open(file_path,'r') as file:
- content = file.read()
- print("文件内容")
- print(content)
- values = []
- with open(file_path, 'r', encoding='utf-8') as file:
- for line in file:
- # 去掉行末的换行符并以 ':' 分割
- parts = line.strip().split(':')
- if len(parts) > 1:
- # 将 ':' 后面的部分储存到变量中
- value = parts[1].strip() # 去掉可能的空格
- values.append(value)
- for value in values:
- print(value)
复制代码 该如何写 |