python读取文件中的一行有效数据

数据格式:
t1.txt

1
1123
1y211
121&*dd99

0000
  01
  23
 0111

  993
  d
    984

代码:

total = []

with open('t1.txt') as fd:
    cur = []
    Data = []
    data = []
    for line in fd:
        line = line.rstrip()
        line = line.lstrip()
        cur.append(line)
        if line.isdigit():
            Data.append(line)
            data.append(int(line))

print(cur)
print(Data)
print(data)

结果:

['1', '1123', '1y211', '121&*dd99', '', '0000', '01', '23', '0111', '', '993', 'd', '984']
['1', '1123', '0000', '01', '23', '0111', '993', '984']
[1, 1123, 0, 1, 23, 111, 993, 984]

你可能感兴趣的:(python)