如何将读取部分 txt 的结果包含到 python 中的数据框中?

How can I include the results of reading parts of a txt into a dataframe in python?

我正在处理一个长的 txt 文件,我有它的结构,并且需要列表中下面代码(形成每一行)的位置,所以我可以将它们放入数据框中并使用他们:

with open('File.txt','r') as file:
lines = file.readlines()
for line in lines:
    a,b = 0,2
    c,d= 2,19
    e,f= 44,52
    g,h= 52,64
 print(line[a:b],line[c:d],line[e:f],line[g:h])

结果在上面的 print() 中就出来了,但我似乎无法使用以下代码将其放入列表中:

list([line[a:b],line[c:d],line[e:f],line[g:h]])

先前代码的结果只是一个包含所有正确位置的列表,但只是我数据中的最后一行

删除打印语句..

list((line[a:b],line[c:d],line[e:f],line[g:h]))

[line[a:b],line[c:d],line[e:f],line[g:h]]

征求意见:

with open('File.txt', 'r') as file:
    lines = file.readlines()
    your_list = []
    a, b = 0, 2
    c, d = 2, 19
    e, f = 44, 52
    g, h = 52, 64

    for line in lines:
        your_list.append([line[a:b], line[c:d], line[e:f], line[g:h]])
import pandas as pd
a, b = 0, 2
c, d = 2, 19
e, f = 44, 52
g, h = 52, 64

##### Read the text file
with open(r"File.txt","r") as file:
    lines = file.readlines()    

##### Convert the text data to Dataframe
df = pd.DataFrame([[line[a:b], line[c:d], line[e:f], line[g:h]] for line in lines])