我正在学习python,我一直在尝试将这个txt文件分成多个文件,这些文件通过每行开头的一个切片字符串进行分组。
目前我有两个问题:
1-字符串可以有5个或6个字符,末尾用空格标记。(如WSON33和JHSF3等...)
下面是我要拆分的文件的示例(第一行是标题):
H24/06/202000003TORDISTD
BWSON33 0803805000000000016400000003250C000002980002415324C1 0000000000000000
BJHSF3 0804608800000000003500000000715V000020280000031810C1 00000000000000002-我带了很多代码,但我不能把所有的东西都放在一起,这样就可以工作了:
这段代码我改编自另一篇文章,它可以分解成多个文件,但在我开始写文件之前,它需要对行进行排序,我还需要复制每个文件中的头文件,而不是将其孤立在一个文件中。
with open('tordist.txt', 'r') as fin:
# group each line in input file by first part of split
for i, (k, g) in enumerate(itertools.groupby(fin, lambda l: l.split()[0]),1):
# create file to write to suffixed with group number - start = 1
with open('{0} tordist.txt'.format(i), 'w') as fout:
# for each line in group write it to file
for line in g:
fout.write(line.strip() + '\n')发布于 2020-06-27 07:09:17
因此,据我所知,您有一个包含许多行的文本文件,其中每行都以5到6个字符的短字符串开头。这听起来像是希望所有以相同字符串开头的行都放到同一个文件中,这样在代码运行后,新文件的数量与唯一的起始字符串数量一样多。这是准确的吗?
和您一样,我也是python的新手,所以我相信有更简洁的方法可以做到这一点。下面的代码多次遍历该文件,并在文本和python文件所在的文件夹中创建新文件。
# code which separates lines in a file by an identifier,
#and makes new files for each identifier group
filename = input('type filename')
if len(filename) < 1:
filename = "mk_newfiles.txt"
filehandle = open(filename)
#This chunck loops through the file, looking at the beginning of each line,
#and adding it to a list of identifiers if it is not on the list already.
Unique = list()
for line in filehandle:
#like Lalit said, split is a simple way to seperate a longer string
line = line.split()
if line[0] not in Unique:
Unique.append(line[0])
#For each item in the list of identifiers, this code goes through
#the file, and if a line starts with that identifier then it is
#added to a new file.
for item in Unique:
#this 'if' skips the header, which has a '/' in it
if '/' not in item:
# the .seek(0) 'rewinds' the iteration variable, which is apperently needed
#needed if looping through files multiple times
filehandle.seek(0)
#makes new file
newfile = open(str(item) + ".txt","w+")
#inserts header, and goes to next line
newfile.write(Unique[0])
newfile.write('\n')
#goes through old file, and adds relevant lines to new file
for line in filehandle:
split_line = line.split()
if item == split_line[0]:
newfile.write(line)
print(Unique)https://stackoverflow.com/questions/62601601
复制相似问题