我在python 3中有以下代码。我试图读取文本文件并输出数值列表。在搜索大量pdf发票时,将使用这些值。
以下是我对文本文件部分的内容:
txt_numbers = []
for file in os.listdir(my_path):
if file[-3:] == "txt":
with open(my_path + file, 'r') as txt_file:
txt = txt_file.readlines()
for line in txt:
# get number between quotes
num = re.findall(r'(?<=").*?(?=")', line)
txt_numbers.append(num)
for c, value in enumerate(txt_numbers, 1):
print(c, value)
以下是输出: [[],[&#39; 51,500.00&#39;],[&#39; 6,000.00&#39;],[&#39; 77,000.00&#39;],[&#39; 37,000.00&#39; ]
问题:如何删除&#34; [&#34;从列表中。我想只是[&#39; 51,500.00&#39;,&#39; 6,000.00&#39;等...]
我尝试过new_text_numbers =(&#34;,&#34; .join(txt_numbers))然后打印(new_text_numbers)
答案 0 :(得分:0)
问题:我在附加一个列表列表,这在python中是允许的,而不是我想要的。
添加了行:
new_num = (", ".join(num))
txt_numbers.append(new_num)
解决方案:
txt_numbers = []
for file in os.listdir(my_path):
if file[-3:] == "txt":
with open(my_path + file, 'r') as txt_file:
txt = txt_file.readlines()
for line in txt:
# get number between quotes
num = re.findall(r'(?<=").*?(?=")', line)
new_num = (", ".join(num))
txt_numbers.append(new_num)
for c, value in enumerate(txt_numbers, 1):
print(c, value)