我在 Python 中有这个 txt 文件,我想将 DeviceID 和 Size 提取到 2 个单独的变量中。该文件看起来像这样:DeviceID SizeF: 7790379008我使用了这段代码:with open('Disk.txt', 'rb') as f: contents = f.read() print(contents)f.close()但我不断得到一些编码输出:
1 回答
12345678_0001
TA贡献1802条经验 获得超5个赞
使用r代替,rb因为您的文件是文本文件。
with open('Disk.txt', 'r') as f:
contents = f.read() for line in contents[1:] :
line = line.replace(' ','')
line = line.split(':')
DeviceID = line[0]+':'
Size = int(line[1])
print(DeviceID,Size)添加回答
举报
0/150
提交
取消
