首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Python分割/切分文本文件

用Python分割/切分文本文件
EN

Stack Overflow用户
提问于 2020-06-27 03:27:56
回答 1查看 56关注 0票数 0

我正在学习python,我一直在尝试将这个txt文件分成多个文件,这些文件通过每行开头的一个切片字符串进行分组。

目前我有两个问题:

1-字符串可以有5个或6个字符,末尾用空格标记。(如WSON33和JHSF3等...)

下面是我要拆分的文件的示例(第一行是标题):

代码语言:javascript
复制
H24/06/202000003TORDISTD 
BWSON33      0803805000000000016400000003250C000002980002415324C1 0000000000000000
BJHSF3       0804608800000000003500000000715V000020280000031810C1 0000000000000000

2-我带了很多代码,但我不能把所有的东西都放在一起,这样就可以工作了:

这段代码我改编自另一篇文章,它可以分解成多个文件,但在我开始写文件之前,它需要对行进行排序,我还需要复制每个文件中的头文件,而不是将其孤立在一个文件中。

代码语言:javascript
复制
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')
EN

回答 1

Stack Overflow用户

发布于 2020-06-27 07:09:17

因此,据我所知,您有一个包含许多行的文本文件,其中每行都以5到6个字符的短字符串开头。这听起来像是希望所有以相同字符串开头的行都放到同一个文件中,这样在代码运行后,新文件的数量与唯一的起始字符串数量一样多。这是准确的吗?

和您一样,我也是python的新手,所以我相信有更简洁的方法可以做到这一点。下面的代码多次遍历该文件,并在文本和python文件所在的文件夹中创建新文件。

代码语言:javascript
复制
# 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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62601601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档