首页 > 解决方案 > 将 .txt 文件的特定行(包含大量行)写入新的 txt 文件

问题描述

我有一个张量格式的文本(.txt)文件,如下所示:

[tensor([[5.7744e+02, 1.7730e+02, 6.2396e+02, 2.1678e+02, 9.9988e-01],
    [5.8410e+02, 1.7532e+02, 6.2769e+02, 2.1083e+02, 9.9969e-01],
    [6.4279e+02, 1.7254e+02, 6.7784e+02, 2.0478e+02, 9.9957e-01],
    [6.9179e+02, 1.6719e+02, 8.2207e+02, 2.6694e+02, 9.9954e-01],
   [1.0050e+03, 2.9167e+02, 1.0792e+03, 3.7500e+02, 5.7740e-05]],
   device='cuda:0')]
[tensor([[7.6615e+02, 1.7577e+02, 8.2579e+02, 2.0013e+02, 9.9981e-01],
    [7.7214e+02, 1.7320e+02, 8.3133e+02, 1.9800e+02, 9.9901e-01],
    [6.3909e+02, 1.7303e+02, 6.6037e+02, 1.8797e+02, 9.9790e-01],
    [7.6184e+02, 1.7468e+02, 8.3014e+02, 2.0274e+02, 9.9395e-01],
   [6.9179e+02, 1.6719e+02, 8.2207e+02, 2.6694e+02, 9.9954e-01],
   [1.0050e+03, 2.9167e+02, 1.0792e+03, 3.7500e+02, 5.7740e-05]],
   device='cuda:0')]

如何将每个张量写入新的 (.txt) 文件。例如我想要:

[tensor([[5.7744e+02, 1.7730e+02, 6.2396e+02, 2.1678e+02, 9.9988e-01],
    [5.8410e+02, 1.7532e+02, 6.2769e+02, 2.1083e+02, 9.9969e-01],
    [6.4279e+02, 1.7254e+02, 6.7784e+02, 2.0478e+02, 9.9957e-01],
    [6.9179e+02, 1.6719e+02, 8.2207e+02, 2.6694e+02, 9.9954e-01],
   [1.0050e+03, 2.9167e+02, 1.0792e+03, 3.7500e+02, 5.7740e-05]],
   device='cuda:0')]

这个张量在文件 one.txt 中,另一个张量在 two.txt 中,依此类推。

标签: pythontensor

解决方案


使用 Matrkeenerh 的建议,如果我们将您的文件按原样放置,input.txt那么您可以做一些非常简单的事情。

file_in = 'input.txt'

counter = 0 # counter used ofr the output files

with open(file_in, 'r') as fin:    
    line = fin.readline() # read file in line by line
    while line: # so long as line is not empty,
                # if you do have blank lines in your file
                # you'd need to do something else
        if 'tensor' in line:
            # the very first time there won't be a fout so use try
            # there are other options, like "if count > 0", etc.
            try:
                fout.close() # close the old file
            except:
                pass
            # augment the counter and open a new file
            counter += 1 
            fout = open('output_%03d.txt' % counter, 'w')
        fout.write(line) # write the line to the output file
        line = fin.readline() # read the next line of the input file
fout.close() # close the last output file

这将使用您要查找的输出创建文件,如output_001.txt,等。output_002.txt

请注意,我在上面选择逐行读取文件。如果文件不大,你可以用类似的东西一次读完

with open(file_in, 'r'):
      lines = fid.readlines()
for line in lines:
      if 'tensor' in line:
            ...
            ...
      fout.write(line)
fout.close()

有可能更简单的方法来做到这一点。例如,如果您的文件是一致的,并且每个tensor条目位于输入文件的 6 行,那么您可以每 6 行开始一个新文件,例如。或者您可以将所有内容保存在一个巨大的文件中,知道要在文件中查找的位置,因为条目n将从在线开始(n-1) * 6 + 1


推荐阅读