我有以下格式的文本文件
国家:加拿大-190605-00001
读数
读数
读数
读数
读数
读数
读数
读数
分隔符
国家:加拿大-190605-00002
读数
读数
读数
读数
读数
读数
读数
读数
分隔符
我能够用下面的代码在分隔符处将文本文件分割成多个文件。我的问题是如何将输出文件名命名为Cntry- 190605 -00001.txt,Cntry-190605-00002.txt,Cntry-190605-00003.txt........Cntry-190605-00020.txt,其中Cntry - type,190605- date,00008 =该天的递增顺序号。(每天从头开始)?我正在考虑使用正则表达式来分配输出文件名,但是下一个输出文件的日期和读取标识符将如何更改呢?
仅在Python中寻求解决方案。谢谢。
input_file = "Test.txt"
with open(input_file, "r") as f:
op = []
i = 1
for line in f:
if line.strip():
op.append(line)
if line.strip() == "Delimiter":
output = open(input_file + '%d.txt' % i,'w')
output.write(''.join(op))
output.close()
i+=1
op = []当前,我的输出文件是
Test.txt1
Test.txt2
Test.txt3预期产出为
Cntry-190605-00001.txt
Cntry-190605-00002.txt
Cntry-190605-00003.txt发布于 2019-06-18 17:35:27
我将在以Country开头的行上打开一个新文件,并将所有内容复制到找到Delimiter为止:
with open(input_file) as f:
copy = False
out = None
for line in f:
if copy:
_ = out.write(line)
if line.strip() == 'Delimiter':
out.close()
copy = False
elif line.strip().startswith('Country'):
file = line.split(':', 1)[1].split()[0]
out = open(file + '.txt', 'w')
_ = out.write(line)
copy = True
if out and not out.closed:
out.close()发布于 2019-06-18 17:06:46
为此,您需要从文本文件本身获取文件名。看看这是否有效:
output_filename = ''
for line in f:
if line.strip():
op.append(line)
if(line.strip().split(:)[0] == "Country"):
output_filename = line.split(:)[1].strip()
if line.strip() == "Delimiter":
output = open(output_filename,'w')发布于 2019-06-18 17:30:36
使用re版本,通过提供的数据,它创建了两个文件Cntry-190605-00001.txt和Cntry-190605-00002.txt,每个文件都有各自的数据。
import re
data = '''Country: Cntry-190605-00001
Readings
Readings
Readings
Delimiter
Country: Cntry-190605-00002
Readings
Readings
Readings
Delimiter'''
names = re.findall(r'Country: (Cntry-\d+-\d+)', data)
for name, p in zip(names, re.findall(r'(.*?Delimiter)\s*', data, flags=re.DOTALL)):
with open(name + '.txt', 'w') as f_out:
f_out.write(p)https://stackoverflow.com/questions/56653818
复制相似问题