如何读取多行传播的文件?

问题描述 投票:0回答:4

我文件中的行分布在多行上。在我的文件的下一个块中,第一行从0.0000000000000000E + 00开始,而第二行从1.5625000000000000E-02开始。如何在1.5625000000000000E-02之前将数字从0.0000000000000000E + 00读取为1?

我正在尝试numpy的genfromtxt()函数和pandas的read_csv(),但我还没有能够将系统的内容传达给我。

#I have put quotation marks here just to indicate the start and end of rows. They
#are not part of the file.

"0.0000000000000000E+00 

00000000     4.9999998882412910E-03       8.7714487508765548E-03  

00000001     5.0000002374872565E-04       5.0877144875087654E-01"


"1.5625000000000000E-02 

00000000     4.9999998882412910E-03       8.4622513106357884E-03 

00000001     5.0000002374872565E-04       5.0864039953085094E-01"

正确读取后,我的输入数组看起来像:

0.0000000000000000E+00   00000000    4.9999998882412910E-03       8.7714487508765548E-03      00000001   5.0000002374872565E-04       5.0877144875087654E-01

1.5625000000000000E-02   00000000    4.9999998882412910E-03       8.4622513106357884E-03      00000001   5.0000002374872565E-04       5.0864039953085094E-01
python pandas numpy file-read
4个回答
0
投票

这应该适用于正则表达式包。

text = """

0.0000000000000000E+00 

00000000     4.9999998882412910E-03       8.7714487508765548E-03  

00000001     5.0000002374872565E-04       5.0877144875087654E-01


1.5625000000000000E-02 

00000000     4.9999998882412910E-03       8.4622513106357884E-03 

00000001     5.0000002374872565E-04       5.0864039953085094E-01"""

码:

import re
xx = re.split(pattern="\n\n\n", string=text)

for xy in xx:
    xy = re.sub(pattern="\s+", repl=" ", string=xy)
    print(xy)
    print("*"*55)

输出:

0.0000000000000000E+00 00000000 4.9999998882412910E-03 8.7714487508765548E-03 00000001 5.0000002374872565E-04 5.0877144875087654E-01
*******************************************************
1.5625000000000000E-02 00000000 4.9999998882412910E-03 8.4622513106357884E-03 00000001 5.0000002374872565E-04 5.0864039953085094E-01
*******************************************************

0
投票

假设你想在输出数据中有7行,这就是你的file。所以这里是如何解析pandas数据帧:

import pandas as pd

with open('temp.txt') as f:
    d = f.read().split()

data = {'col1': [], 'col2': [], 'col3': [], 'col4': [], 'col5': [], 'col6': [], 'col7': []}
for i in range(0, len(d), 7):
    for j in range(7):
        data['col{}'.format(j+1)].append(d[j])

df = pd.DataFrame(data)

输出:

enter image description here


0
投票

下面的代码应该正确解析文件的内容:

import re
import pandas

sample = """0.0000000000000000E+00 

00000000     4.9999998882412910E-03       8.7714487508765548E-03  

00000001     5.0000002374872565E-04       5.0877144875087654E-01


1.5625000000000000E-02 

00000000     4.9999998882412910E-03       8.4622513106357884E-03 

00000001     5.0000002374872565E-04       5.0864039953085094E-01
"""


def load_matrix(content):
    lines = (line for line in content.splitlines() if len(line.strip()) > 0)

    rows = list()
    row = list()
    for line in lines:
        fields = line.split()
        is_continuation = re.match(r'^\d{8}$', fields[0])
        if is_continuation:
            row += [float(value) for value in fields[1:]]

        else:
            if (len(row) > 0):
                rows.append(row)

            row = [float(value) for value in fields]

    rows.append(row)
    return pandas.DataFrame(rows)

print(load_matrix(sample))

显示:

          0      1         2       3         4
0  0.000000  0.005  0.008771  0.0005  0.508771
1  0.015625  0.005  0.008462  0.0005  0.508640

0
投票

输出为两个列表:

import re

file_object = open("over.txt",'rU')

df1=[]
df2=[]
content = ''

try:
    for line in file_object:
        content = content + line
finally:
     file_object.close()

words = re.split(pattern="\n\n\n", string=content)

num = re.sub(pattern="\s+", repl=",", string=words[0])
for i in num.split(","):
    df1.append(float(i))

num = re.sub(pattern="\s+", repl=",", string=words[1])
for i in num.split(","):
    df2.append(float(i))

print df1
print df2

输出:

[0.0, 0.0, 0.004999999888241291, 0.008771448750876555, 1.0, 0.0005000000237487257, 0.5087714487508765]
[0.015625, 0.0, 0.004999999888241291, 0.008462251310635788, 1.0, 0.0005000000237487257, 0.5086403995308509]
© www.soinside.com 2019 - 2024. All rights reserved.