我在列表中读了很多txt文件。这是我的代码
import glob
path = '/Users/xccxken/Desktop/NNRelease/paperVersion/*.txt'
files = glob.glob(path)
for name in files:
with open(name) as f:
for line in f:
split = line.split()
if split and all('#' not in i for i in split):
r = split
a = []
b = []
for line in r:
a.append(r[0])
b.append(r[1])
,输出就像这样
['water', 'water']
['shortage', 'shortage']
我想要的输出是两个列表a和b,如下所示:
a = [class, company,...,XXXX]
b = [size, size,..., YYYY]
每个文本文件如下:
请帮我修复代码
答案 0 :(得分:0)
a = []
b = []
for name in files:
with open(name) as f:
for line in f:
split = line.split()
if split and all('#' not in i for i in split):
a.append(split[0])
b.append(split[1])
print(a) # prints ['class', 'company', 'family', 'sandwich']
print(b) # prints ['size', 'size', 'size', 'size']