阅读一个文本文件并在Python中调用每个数组。

huangapple go评论70阅读模式
英文:

Reading a text file and calling each array in Python

问题

I am reading a .txt file using f.read. But I also want to assign it as a list and call each array within this list. I present the current and expected output.

f = open("Test.txt", "r")
print("f read =", f.read())
print("f 0 =", f[0])

The current output is

f read = [array([ 4.24805745e+002,  0.00000000e+000,  0.00000000e+000,
         4.02753575e+002,  3.79606698e+002,  0.00000000e+000,
         3.01629825e+002,  2.73930748e+002,  2.73930748e+002,
         1.44270041e-014, -1.19840421e-014,  2.48466223e+002,
         4.07808390e-014,  2.23084563e+002,  2.23084563e+002,
         1.95133833e+002, -1.90524280e-014,  1.13675881e+002,
         1.60355972e-023,  8.87020057e+001,  6.51230922e+001,
         8.87020057e+001,  1.00000000e-100])
 array([ 4.24805745e+002,  0.00000000e+000,  0.00000000e+000,
         3.96370317e+002,  3.82498020e+002,  0.00000000e+000,
         3.77200451e+002,  3.22917850e+002,  3.49391875e+002,
         3.24649000e+002,  3.06639099e+002,  3.06639099e+002,
         3.06639099e+002,  2.74439993e+002,  3.06639099e+002,
         2.42357202e+002,  2.42357202e+002,  2.26139235e+002,
         3.06639099e+002,  2.22347429e+002,  1.64667856e+002,
        -1.26698275e-013,  1.69645075e+002,  1.07059051e+002,
         1.95176276e+002,  1.00000000e-100])                 ]

The expected output is

f read = [array([ 4.24805745e+002,  0.00000000e+000,  0.00000000e+000,
         4.02753575e+002,  3.79606698e+002,  0.00000000e+000,
         3.01629825e+002,  2.73930748e+002,  2.73930748e+002,
         1.44270041e-014, -1.19840421e-014,  2.48466223e+002,
         4.07808390e-014,  2.23084563e+002,  2.23084563e+002,
         1.95133833e+002, -1.90524280e-014,  1.13675881e+002,
         1.60355972e-023,  8.87020057e+001,  6.51230922e+001,
         8.87020057e+001,  1.00000000e-100])
 array([ 4.24805745e+002,  0.00000000e+000,  0.00000000e+000,
         3.96370317e+002,  3.82498020e+002,  0.00000000e+000,
         3.77200451e+002,  3.22917850e+002,  3.49391875e+002,
         3.24649000e+002,  3.06639099e+002,  3.06639099e+002,
         3.06639099e+002,  2.74439993e+002,  3.06639099e+002,
         2.42357202e+002,  2.42357202e+002,  2.26139235e+002,
         3.06639099e+002,  2.22347429e+002,  1.64667856e+002,
        -1.26698275e-013, 1.69645075e+002, 1.07059051e+002,
         1.95176276e+002, 1.00000000e-100])                 ]

f 0 = array([ 4.24805745e+002, 0.00000000e+000, 0.00000000e+000,
         4.02753575e+002, 3.79606698e+002, 0.00000000e+000,
         3.01629825e+002, 2.73930748e+002, 2.73930748e+002,
         1.44270041e-014, -1.19840421e-014, 2.48466223e+002,
         4.07808390e-014, 2.23084563e+002, 2.23084563e+002,
         1.95133833e+002, -1.90524280e-014, 1.13675881e+002,
         1.60355972e-023, 8.87020057e+001, 6.51230922e+001,
         8.87020057e+001, 1.00000000e-100])
英文:

I am reading a .txt file using f.read. But I also want to assign it as a list and call each array within this list . I present the current and expected output.

f = open("Test.txt", "r")
print("f read =",f.read()) 
print("f 0 =",f[0])

The current output is

f read = [array([ 4.24805745e+002,  0.00000000e+000,  0.00000000e+000,
         4.02753575e+002,  3.79606698e+002,  0.00000000e+000,
         3.01629825e+002,  2.73930748e+002,  2.73930748e+002,
         1.44270041e-014, -1.19840421e-014,  2.48466223e+002,
         4.07808390e-014,  2.23084563e+002,  2.23084563e+002,
         1.95133833e+002, -1.90524280e-014,  1.13675881e+002,
         1.60355972e-023,  8.87020057e+001,  6.51230922e+001,
         8.87020057e+001,  1.00000000e-100])
 array([ 4.24805745e+002,  0.00000000e+000,  0.00000000e+000,
         3.96370317e+002,  3.82498020e+002,  0.00000000e+000,
         3.77200451e+002,  3.22917850e+002,  3.49391875e+002,
         3.24649000e+002,  3.06639099e+002,  3.06639099e+002,
         3.06639099e+002,  2.74439993e+002,  3.06639099e+002,
         2.42357202e+002,  2.42357202e+002,  2.26139235e+002,
         3.06639099e+002,  2.22347429e+002,  1.64667856e+002,
        -1.26698275e-013,  1.69645075e+002,  1.07059051e+002,
         1.95176276e+002,  1.00000000e-100])                 ]


Traceback (most recent call last):

  in <module>
    print("f 0 =",f[0])

TypeError: '_io.TextIOWrapper' object is not subscriptable

The expected output is

f read = [array([ 4.24805745e+002,  0.00000000e+000,  0.00000000e+000,
         4.02753575e+002,  3.79606698e+002,  0.00000000e+000,
         3.01629825e+002,  2.73930748e+002,  2.73930748e+002,
         1.44270041e-014, -1.19840421e-014,  2.48466223e+002,
         4.07808390e-014,  2.23084563e+002,  2.23084563e+002,
         1.95133833e+002, -1.90524280e-014,  1.13675881e+002,
         1.60355972e-023,  8.87020057e+001,  6.51230922e+001,
         8.87020057e+001,  1.00000000e-100])
 array([ 4.24805745e+002,  0.00000000e+000,  0.00000000e+000,
         3.96370317e+002,  3.82498020e+002,  0.00000000e+000,
         3.77200451e+002,  3.22917850e+002,  3.49391875e+002,
         3.24649000e+002,  3.06639099e+002,  3.06639099e+002,
         3.06639099e+002,  2.74439993e+002,  3.06639099e+002,
         2.42357202e+002,  2.42357202e+002,  2.26139235e+002,
         3.06639099e+002,  2.22347429e+002,  1.64667856e+002,
        -1.26698275e-013,  1.69645075e+002,  1.07059051e+002,
         1.95176276e+002,  1.00000000e-100])                 ]

f 0 = array([ 4.24805745e+002,  0.00000000e+000,  0.00000000e+000,
         4.02753575e+002,  3.79606698e+002,  0.00000000e+000,
         3.01629825e+002,  2.73930748e+002,  2.73930748e+002,
         1.44270041e-014, -1.19840421e-014,  2.48466223e+002,
         4.07808390e-014,  2.23084563e+002,  2.23084563e+002,
         1.95133833e+002, -1.90524280e-014,  1.13675881e+002,
         1.60355972e-023,  8.87020057e+001,  6.51230922e+001,
         8.87020057e+001,  1.00000000e-100])

</details>


# 答案1
**得分**: 0

假设你的`Test.txt`文件包含以下内容:

```text
[array([ 4.24805745e+002,  0.00000000e+000,  0.00000000e+000,
         4.02753575e+002,  3.79606698e+002,  0.00000000e+000,
         3.01629825e+002,  2.73930748e+002,  2.73930748e+002,
         1.44270041e-014, -1.19840421e-014,  2.48466223e+002,
         4.07808390e-014,  2.23084563e+002,  2.23084563e+002,
         1.95133833e+002, -1.90524280e-014,  1.13675881e+002,
         1.60355972e-023,  8.87020057e+001,  6.51230922e+001,
         8.87020057e+001,  1.00000000e-100])
 array([ 4.24805745e+002,  0.00000000e+000,  0.00000000e+000,
         3.96370317e+002,  3.82498020e+002,  0.00000000e+000,
         3.77200451e+002,  3.22917850e+002,  3.49391875e+002,
         3.24649000e+002,  3.06639099e+002,  3.06639099e+002,
         3.06639099e+002,  2.74439993e+002,  3.06639099e+002,
         2.42357202e+002,  2.42357202e+002,  2.26139235e+002,
         3.06639099e+002,  2.22347429e+002,  1.64667856e+002,
        -1.26698275e-013,  1.69645075e+002,  1.07059051e+002,
         1.95176276e+002,  1.00000000e-100])                 ]

然后,假设所涉及的数组是NumPy数组,你可以这样操作:

import numpy as np

# 读取整个文件
with open("Test.txt", "r") as f:
    inputdata = f.read()

# 使用eval解析数据
data = eval(
    inputdata
    .strip()  # 去掉末尾的空白字符
    .replace("\n", "")  # 用空字符串替换换行符
    .replace("),", "),\n")  # 在数组之间添加逗号
    {"array": np.array}  # 确保它知道将array解析为NumPy数组
)

print(data[0])

请注意,eval函数可以执行任意代码,因此只有在信任输入文件的来源时才使用它。

英文:

Assuming your Test.txt file contains:

[array([ 4.24805745e+002,  0.00000000e+000,  0.00000000e+000,
         4.02753575e+002,  3.79606698e+002,  0.00000000e+000,
         3.01629825e+002,  2.73930748e+002,  2.73930748e+002,
         1.44270041e-014, -1.19840421e-014,  2.48466223e+002,
         4.07808390e-014,  2.23084563e+002,  2.23084563e+002,
         1.95133833e+002, -1.90524280e-014,  1.13675881e+002,
         1.60355972e-023,  8.87020057e+001,  6.51230922e+001,
         8.87020057e+001,  1.00000000e-100])
 array([ 4.24805745e+002,  0.00000000e+000,  0.00000000e+000,
         3.96370317e+002,  3.82498020e+002,  0.00000000e+000,
         3.77200451e+002,  3.22917850e+002,  3.49391875e+002,
         3.24649000e+002,  3.06639099e+002,  3.06639099e+002,
         3.06639099e+002,  2.74439993e+002,  3.06639099e+002,
         2.42357202e+002,  2.42357202e+002,  2.26139235e+002,
         3.06639099e+002,  2.22347429e+002,  1.64667856e+002,
        -1.26698275e-013,  1.69645075e+002,  1.07059051e+002,
         1.95176276e+002,  1.00000000e-100])                 ]

then, assuming the arrays in question are NumPy arrays, you can do:

import numpy as np

# read in the whole file
with open(&quot;Test.txt&quot;, &quot;r&quot;) as f:
    inputdata = f.read()

# parse the data with eval
data = eval(
    inputdata
    .strip()  # remove trailing whitespace
    .replace(&quot;\n&quot;, &quot;&quot;)  # replace new lines with empty strings
    .replace(&quot;)&quot;, &quot;),&quot;),  # stick a comma between the arrays
    {&quot;array&quot;: np.array}  # make sure it knows to parse array as a NumPy array
)

print(data[0])

array([ 4.24805745e+002,  0.00000000e+000,  0.00000000e+000,
        4.02753575e+002,  3.79606698e+002,  0.00000000e+000,
        3.01629825e+002,  2.73930748e+002,  2.73930748e+002,
        1.44270041e-014, -1.19840421e-014,  2.48466223e+002,
        4.07808390e-014,  2.23084563e+002,  2.23084563e+002,
        1.95133833e+002, -1.90524280e-014,  1.13675881e+002,
        1.60355972e-023,  8.87020057e+001,  6.51230922e+001,
        8.87020057e+001,  1.00000000e-100])

Note that the eval function can execute arbitrary code, so only use it if you trust the source of your input file.

huangapple
  • 本文由 发表于 2023年2月6日 17:51:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359723.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定