英文:
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("Test.txt", "r") as f:
inputdata = f.read()
# parse the data with eval
data = eval(
inputdata
.strip() # remove trailing whitespace
.replace("\n", "") # replace new lines with empty strings
.replace(")", "),"), # stick a comma between the arrays
{"array": 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论