将文本文件中的值转换为 python 中的数组

convert values in text file to array in python

我有一个文本文件 data.txt 看起来是这样的:

ADMAS      8.046E+03 8.206E-03 1.532E+04 1.066E-01 6.982E+06-2.820E+00 \n
ADMAS     -6.868E-03 2.009E+05 1.454E-02 9.516E+05-1.209E+00 6.058E+06 \n 
ADMAS      1.543E+04 9.179E-01 1.459E+06 5.463E+00 3.918E+07-2.904E+01 \n 
ADMAS     -2.267E-01 9.537E+05 3.902E+00 3.071E+07-1.344E+02 1.073E+08 \n 
ADMAS      7.005E+06 2.260E+01 3.978E+07 6.296E+01 7.586E+09-2.125E+03 \n 
ADMAS      1.093E+00 6.052E+06-6.178E+00 1.065E+08-1.416E+03 1.941E+09 \n 
FAMP       3.824E+03 7.120E-02 1.848E+05 7.317E-01 5.406E+06 4.096E+00 \n 
FEPS       9.039E+01 3.571E+02 2.838E+00 3.580E+02 4.098E+01 1.892E+02 \n 

(以此类推)。我只想拥有 ADMAS 值并将它们放在一个 6x6 数组中。 我尝试了以下方法:

filename = "data.txt"
string_fnd_1 = "ADMAS"
textfile = open(filename, 'r')
file_lines = textfile.readlines()
textfile.close()
matches_admas = [line for line in file_lines if string_fnd_1 in line]

我得到以下信息:

['ADMAS      8.046E+03 8.206E-03 1.532E+04 1.066E-01 6.982E+06-2.820E+00\n', 'ADMAS     -6.868E-03 2.009E+05 1.454E-02 9.516E+05-1.209E+00 6.058E+06\n',....]

现在,我想去掉字符串 "ADMAS",将值转换为浮点数并重塑为 6x6 数组。有人知道怎么做吗?帮助将不胜感激。

它不是最干净的,但它似乎有效:

  • 我在减号前创建空格 - 以便稍后在数字之间拆分
  • 我删除了 E - 的空格,因为前一点
  • 的副作用
  • 我用replace(" \n", "")
  • 删除了\n硬写的文字
  • 我也删除了 "next line" 个字符 \n
  • 然后我用 [11:]
  • 删除了前 11 个字符(例如:ADMAS
filename = "data.txt"
string_fnd_1 = "ADMAS"
textfile = open(filename, 'r')
file_lines = textfile.readlines()
textfile.close()
new_file_lines = []
for li in file_lines:
    new_line = li.replace("-", " -").replace("E -", "E-").replace(" \n", "").replace("\n", "")[11:]
    if string_fnd_1 in li:
        new_file_lines.append(new_line)
matches_admas = [line.split() for line in new_file_lines]

print(matches_admas)

输出:

[
    ['8.046E+03', '8.206E-03', '1.532E+04', '1.066E-01', '6.982E+06', '-2.820E+00'],
    ['-6.868E-03', '2.009E+05', '1.454E-02', '9.516E+05', '-1.209E+00', '6.058E+06'],
    ['1.543E+04', '9.179E-01', '1.459E+06', '5.463E+00', '3.918E+07', '-2.904E+01'],
    ['-2.267E-01', '9.537E+05', '3.902E+00', '3.071E+07', '-1.344E+02', '1.073E+08'],
    ['7.005E+06', '2.260E+01', '3.978E+07', '6.296E+01', '7.586E+09', '-2.125E+03'],
    ['1.093E+00', '6.052E+06', '-6.178E+00', '1.065E+08', '-1.416E+03', '1.941E+09']
]

你应该考虑使用 Pandas。

python -m pip install pandas

然后你需要导入 pandas 到你的代码中。

import pandas as pd

然后您需要找到该文件并将其作为数据框导入。

df = pd.read_fwf(YOURPATH + 'data.txt')
#And try to print its head to see if import was successfull
print(df.head())

最后,您应该在数据框的第一列上过滤 'ADAM'。

df.loc[df['column_name'] == ADAM]

根据您的要求,我想出了以下解决方案:

import re
filename = "data.txt"
string_fnd_1 = "ADMAS"

matrix = [] # 6x6 matrix of dtype float
with open(filename, "r") as f:
    for line in f:
        if string_fnd_1 in line:
            # cleaning the bad chars in line
            line = line.strip()
            line = line.strip(" \n")
            line = re.sub(r"ADMAS\s*", "", line)
            line = re.sub(r"(-[0-9]+\.)", r" ", line)

            values = [float(value) for value in line.split()]
            matrix.append(values)

输出将是:

[
[8046.0, 0.008206, 15320.0, 0.1066, 6982000.0, -2.82], 
[-0.006868, 200900.0, 0.01454, 951600.0, -1.209, 6058000.0], 
[15430.0, 0.9179, 1459000.0, 5.463, 39180000.0, -29.04], 
[-0.2267, 953700.0, 3.902, 30710000.0, -134.4, 107300000.0], 
[7005000.0, 22.6, 39780000.0, 62.96, 7586000000.0, -2125.0], 
[1.093, 6052000.0, -6.178, 106500000.0, -1416.0, 1941000000.0]
]

希望它能解决您的问题!