创建 TXT 文件并在 Python 中寻求职位

Creating a TXT file and seeking a position in Python

我已经给出了以下变量:

signal1 = 'speed'
bins1 = [0, 10, 20, 30, 40]

signal2 = 'rpm'
bins2 = [0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500]

hist_result = [ [1, 4, 5, 12], 
                [-5, 8, 9, 0],
                [-6, 7, 11, 19],
                [1, 4, 5, 12], 
                [-5, 8, 9, 0],
                [-6, 7, 11, 19],
                [1, 4, 5, 12], 
                [-5, 8, 9, 0],
                [-6, 7, 11, 19],
              ]

我想创建一个 .TXT 文件,它看起来像这样,带有制表符分隔值:


                speed>= 0   10  20  30
                speed<  10  20  30  40
        rpm>=   rpm<    
        0   500         1   4   5   12   
        500 1000        5   8   9   0
        1000    1500    6   7   11  19
        1500    2000    1   4   5   12
        2000    2500    -5  8   9   0
        2500    3000    -6  7   11  19
        3000    3500    1   4   5   12
        3500    4000    -5  8   9   0
        4000    4500    -6  7   11  19

我写了下面的代码:

#!/usr/bin/env python3

import os
from datetime import datetime
import time

signal1 = 'speed'
bins1 = [0, 10, 20, 30, 40]

signal2 = 'rpm'
bins2 = [0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500]

hist_result = [ [1, 4, 5, 12], 
                [-5, 8, 9, 0],
                [-6, 7, 11, 19],
                [1, 4, 5, 12], 
                [-5, 8, 9, 0],
                [-6, 7, 11, 19],
                [1, 4, 5, 12], 
                [-5, 8, 9, 0],
                [-6, 7, 11, 19],
              ]

filename = f"{datetime.now().strftime('%Y%m%d_%H%M%S')}_{signal1}_results.TXT"

with open(filename, 'w') as f:
   
    # write the bin1 range
    f.write('\n\n\n')
    f.write('\t\t\t\t')
    f.write(signal1 + '>=')

    for bin in bins1[:-1]:
        f.write('\t' + str(bin))
    f.write('\n')
    f.write('\t\t\t\t')

    f.write(signal1 + '<')
    for bin in bins1[1:]:  
        f.write('\t' + str(bin))
    f.write('\n')
    
    # write the bin2 range
    f.write('\t\t')
    f.write(signal2 + '>=' + '\t' + signal2 + '<' + '\n')
    f.write('\t\t') 

    # store the cursor position from where hist result will be written line by line
    track_cursor_pos = []            

    curr = bins2[0]
    for next in bins2[1:]:
        f.write(str(curr) + '\t' + str(next)) 
        track_cursor_pos.append(f.tell()) 
        f.write('\n\t\t')
        curr = next
    f.write('\n')   

    print(track_cursor_pos)  

    i = 0
    # Everything is fine until here 
    # Code below doesn't work as expected!?
    for result in hist_result: 
        f.seek(track_cursor_pos[i], os.SEEK_SET)
        for r in result:            
            f.write('\t' + str(r))
        f.write('\n')
        i += 1

但是,这是生成内容如下所示的 TXT 文件:


                speed>= 0   10  20  30
                speed<  10  20  30  40
        rpm>=   rpm<
        0   500 1   4   5   12
0   -5  8   9   0
00  -6  7   11  19
    1   4   5   12
00  -5  8   9   0
00  -6  7   11  19
    1   4   5   12
00  -5  8   9   0
00  -6  7   11  19

我想我没有正确使用 f.seek()。任何建议将不胜感激。提前致谢。

您无需在文件内部查找即可打印您的数据:

signal1 = 'speed'
bins1 = [0, 10, 20, 30, 40]

signal2 = 'rpm'
bins2 = [0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500]

hist_result = [ [1, 4, 5, 12], 
                [-5, 8, 9, 0],
                [-6, 7, 11, 19],
                [1, 4, 5, 12], 
                [-5, 8, 9, 0],
                [-6, 7, 11, 19],
                [1, 4, 5, 12], 
                [-5, 8, 9, 0],
                [-6, 7, 11, 19],
              ]


with open('data.txt', 'w') as f_out:
    print('\t{signal1}>=\t{bins}'.format(signal1=signal1, bins='\t'.join(map(str,bins1[:-1]))), file=f_out)
    print('\t{signal1}<\t{bins}'.format(signal1=signal1, bins='\t'.join(map(str,bins1[1:]))), file=f_out)
    print('{signal2}>=\t{signal2}<'.format(signal2=signal2))

    for a, b, data in zip(bins2[:-1], bins2[1:], hist_result):
        print(a, b, *data, sep='\t', file=f_out)

创建 data.txt:

        speed>= 0       10      20      30
        speed<  10      20      30      40
rpm>=   rpm<
0       500     1       4       5       12
500     1000    -5      8       9       0
1000    1500    -6      7       11      19
1500    2000    1       4       5       12
2000    2500    -5      8       9       0
2500    3000    -6      7       11      19
3000    3500    1       4       5       12
3500    4000    -5      8       9       0
4000    4500    -6      7       11      19