0

我最近开始使用 Python,现在我遇到了类似于以下的问题。我有一个 txt 文件,其中行没有相同数量的值:

    who you gonna call 555 2368
    56 20 9 7 8
    0 9 7 -789 -9
    -19 -14 0 9 0
    0 -1 0 9 0
   -4.0 -4.1 -4.2 -4.3 -4.4 
   -5.0 -5.1 -5.2 -5.3 -5.4 
   -6.0 -6.1 -6.2 -6.3 -6.4 
   -7.0 -7.1 -7.2 -7.3 -7.4 
  
   +1.0 +1.1 +1.2 +1.3 +1.4
   +2.0 +2.1 +2.2 +2.3 +2.4
   +3.0 +3.1 +3.2 +3.3 +3.4 
   +4.0 +4.1 +4.2 +4.3 +4.4
 
   -6.0 -6.1 -6.2 -6.3 -6.4 
   -7.0 -7.1 -7.2 -7.3 -7.4
   -8.0 -8.1 -8.2 -8.3 -8.4
   -9.0 -9.1 -9.2 -9.3 -9.4

对于第一行,我只需要读取一些值,而对于其他(块),我需要逐块单独读取它们。到目前为止,我写了这样的东西:

import tkinter as tk
from tkinter import filedialog
import numpy as np
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
f = open(file_path)
with open(file_path) as fp:
    first_line = fp.readline()
    phone_number = first_line[0:28]

    second_line = fp.readline()
    nums2 = second_line.split(' ')
    total_elements = (float(nums2[2]))

    first_block = []
    first_block_lines = range(5,int(5+(total_elements/5)))
    for position, line in enumerate(f):
        if position in first_block_lines:
            first_block.append(line)

    second_block = []
    second_block_lines = range(int(5+(total_elements/5)+1), int(5+(total_elements/5)+1+total_elements/5))
    for position, line in enumerate(f):
        if position in second_block_lines:
            second_block.append(line)

在我的真实案例中,我有数百个块,所有块都具有相同数量的行和列。我如何通过积木 ( range(...)) 的方式很糟糕,我想知道如何让它变得更聪明。如果我不清楚,请告诉我。非常感谢。

4

1 回答 1

1

由于您知道块何时开始并且块由空行分隔,因此您可以遍历文件并将每一行附加到列表中。当找到空行时,将行列表附加到主块列表。

试试这个代码:

ss = '''
who you gonna call 555 2368
56 20 9 7 8
0 9 7 -789 -9
-19 -14 0 9 0
0 -1 0 9 0
-4.0 -4.1 -4.2 -4.3 -4.4 
-5.0 -5.1 -5.2 -5.3 -5.4 
-6.0 -6.1 -6.2 -6.3 -6.4 
-7.0 -7.1 -7.2 -7.3 -7.4 

+1.0 +1.1 +1.2 +1.3 +1.4
+2.0 +2.1 +2.2 +2.3 +2.4
+3.0 +3.1 +3.2 +3.3 +3.4 
+4.0 +4.1 +4.2 +4.3 +4.4

-6.0 -6.1 -6.2 -6.3 -6.4 
-7.0 -7.1 -7.2 -7.3 -7.4
-8.0 -8.1 -8.2 -8.3 -8.4
-9.0 -9.1 -9.2 -9.3 -9.4
'''.strip()

with open ('data.txt', 'w') as f: f.write(ss)  # write test file

####################################


with open('data.txt') as fp:
    first_line = fp.readline()
    phone_number = first_line[0:28]

    second_line = fp.readline()
    nums2 = second_line.split(' ')
    total_elements = (float(nums2[2]))

# read blocks
blocks = []  # list of lists of lines
startline=5
blockcnt=0
lst = []  # one block
with open('data.txt') as fp:
    for x in range(startline): fp.readline() # before blocks
    while True:  # until end of file
        ln = fp.readline()
        if not ln: break  # end of file
        ln = ln.strip() # strip \n
        if ln.strip() == "":  # found space between blocks
            blocks.append(lst)  # add to main block list
            lst = []  # reset for next block
        else:
            lst.append(ln) # add line to block
blocks.append(lst)  # last block

for b in blocks:  # each block
    print(b)

输出

['-4.0 -4.1 -4.2 -4.3 -4.4', '-5.0 -5.1 -5.2 -5.3 -5.4', '-6.0 -6.1 -6.2 -6.3 -6.4', '-7.0 -7.1 -7.2 -7.3 -7.4']
['+1.0 +1.1 +1.2 +1.3 +1.4', '+2.0 +2.1 +2.2 +2.3 +2.4', '+3.0 +3.1 +3.2 +3.3 +3.4', '+4.0 +4.1 +4.2 +4.3 +4.4']
['-6.0 -6.1 -6.2 -6.3 -6.4', '-7.0 -7.1 -7.2 -7.3 -7.4', '-8.0 -8.1 -8.2 -8.3 -8.4', '-9.0 -9.1 -9.2 -9.3 -9.4']

如果您想更进一步并将值转换为浮点数,请使用以下代码附加每一行:

lst.append([float(f) for f in ln.split(' ')]) # add line to block

输出

[[-4.0, -4.1, -4.2, -4.3, -4.4], [-5.0, -5.1, -5.2, -5.3, -5.4], [-6.0, -6.1, -6.2, -6.3, -6.4], [-7.0, -7.1, -7.2, -7.3, -7.4]]
[[1.0, 1.1, 1.2, 1.3, 1.4], [2.0, 2.1, 2.2, 2.3, 2.4], [3.0, 3.1, 3.2, 3.3, 3.4], [4.0, 4.1, 4.2, 4.3, 4.4]]
[[-6.0, -6.1, -6.2, -6.3, -6.4], [-7.0, -7.1, -7.2, -7.3, -7.4], [-8.0, -8.1, -8.2, -8.3, -8.4], [-9.0, -9.1, -9.2, -9.3, -9.4]]

如果要命名块,可以将列表转换为字典:

# convert to dictionary
dd = {'block_' + str(i):lst for i,lst in enumerate(blocks)}
print(dd)

输出

{'block_0': [[-4.0, -4.1, -4.2, -4.3, -4.4], [-5.0, -5.1, -5.2, -5.3, -5.4], [-6.0, -6.1, -6.2, -6.3, -6.4], [-7.0, -7.1, -7.2, -7.3, -7.4]], 
 'block_1': [[1.0, 1.1, 1.2, 1.3, 1.4], [2.0, 2.1, 2.2, 2.3, 2.4], [3.0, 3.1, 3.2, 3.3, 3.4], [4.0, 4.1, 4.2, 4.3, 4.4]], 
 'block_2': [[-6.0, -6.1, -6.2, -6.3, -6.4], [-7.0, -7.1, -7.2, -7.3, -7.4], [-8.0, -8.1, -8.2, -8.3, -8.4], [-9.0, -9.1, -9.2, -9.3, -9.4]]}
于 2020-10-07T17:43:22.600 回答