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

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

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.

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

The current output is

  1. f read = [array([ 4.24805745e+002, 0.00000000e+000, 0.00000000e+000,
  2. 4.02753575e+002, 3.79606698e+002, 0.00000000e+000,
  3. 3.01629825e+002, 2.73930748e+002, 2.73930748e+002,
  4. 1.44270041e-014, -1.19840421e-014, 2.48466223e+002,
  5. 4.07808390e-014, 2.23084563e+002, 2.23084563e+002,
  6. 1.95133833e+002, -1.90524280e-014, 1.13675881e+002,
  7. 1.60355972e-023, 8.87020057e+001, 6.51230922e+001,
  8. 8.87020057e+001, 1.00000000e-100])
  9. array([ 4.24805745e+002, 0.00000000e+000, 0.00000000e+000,
  10. 3.96370317e+002, 3.82498020e+002, 0.00000000e+000,
  11. 3.77200451e+002, 3.22917850e+002, 3.49391875e+002,
  12. 3.24649000e+002, 3.06639099e+002, 3.06639099e+002,
  13. 3.06639099e+002, 2.74439993e+002, 3.06639099e+002,
  14. 2.42357202e+002, 2.42357202e+002, 2.26139235e+002,
  15. 3.06639099e+002, 2.22347429e+002, 1.64667856e+002,
  16. -1.26698275e-013, 1.69645075e+002, 1.07059051e+002,
  17. 1.95176276e+002, 1.00000000e-100]) ]

The expected output is

  1. f read = [array([ 4.24805745e+002, 0.00000000e+000, 0.00000000e+000,
  2. 4.02753575e+002, 3.79606698e+002, 0.00000000e+000,
  3. 3.01629825e+002, 2.73930748e+002, 2.73930748e+002,
  4. 1.44270041e-014, -1.19840421e-014, 2.48466223e+002,
  5. 4.07808390e-014, 2.23084563e+002, 2.23084563e+002,
  6. 1.95133833e+002, -1.90524280e-014, 1.13675881e+002,
  7. 1.60355972e-023, 8.87020057e+001, 6.51230922e+001,
  8. 8.87020057e+001, 1.00000000e-100])
  9. array([ 4.24805745e+002, 0.00000000e+000, 0.00000000e+000,
  10. 3.96370317e+002, 3.82498020e+002, 0.00000000e+000,
  11. 3.77200451e+002, 3.22917850e+002, 3.49391875e+002,
  12. 3.24649000e+002, 3.06639099e+002, 3.06639099e+002,
  13. 3.06639099e+002, 2.74439993e+002, 3.06639099e+002,
  14. 2.42357202e+002, 2.42357202e+002, 2.26139235e+002,
  15. 3.06639099e+002, 2.22347429e+002, 1.64667856e+002,
  16. -1.26698275e-013, 1.69645075e+002, 1.07059051e+002,
  17. 1.95176276e+002, 1.00000000e-100]) ]
  18. f 0 = array([ 4.24805745e+002, 0.00000000e+000, 0.00000000e+000,
  19. 4.02753575e+002, 3.79606698e+002, 0.00000000e+000,
  20. 3.01629825e+002, 2.73930748e+002, 2.73930748e+002,
  21. 1.44270041e-014, -1.19840421e-014, 2.48466223e+002,
  22. 4.07808390e-014, 2.23084563e+002, 2.23084563e+002,
  23. 1.95133833e+002, -1.90524280e-014, 1.13675881e+002,
  24. 1.60355972e-023, 8.87020057e+001, 6.51230922e+001,
  25. 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.

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

The current output is

  1. f read = [array([ 4.24805745e+002, 0.00000000e+000, 0.00000000e+000,
  2. 4.02753575e+002, 3.79606698e+002, 0.00000000e+000,
  3. 3.01629825e+002, 2.73930748e+002, 2.73930748e+002,
  4. 1.44270041e-014, -1.19840421e-014, 2.48466223e+002,
  5. 4.07808390e-014, 2.23084563e+002, 2.23084563e+002,
  6. 1.95133833e+002, -1.90524280e-014, 1.13675881e+002,
  7. 1.60355972e-023, 8.87020057e+001, 6.51230922e+001,
  8. 8.87020057e+001, 1.00000000e-100])
  9. array([ 4.24805745e+002, 0.00000000e+000, 0.00000000e+000,
  10. 3.96370317e+002, 3.82498020e+002, 0.00000000e+000,
  11. 3.77200451e+002, 3.22917850e+002, 3.49391875e+002,
  12. 3.24649000e+002, 3.06639099e+002, 3.06639099e+002,
  13. 3.06639099e+002, 2.74439993e+002, 3.06639099e+002,
  14. 2.42357202e+002, 2.42357202e+002, 2.26139235e+002,
  15. 3.06639099e+002, 2.22347429e+002, 1.64667856e+002,
  16. -1.26698275e-013, 1.69645075e+002, 1.07059051e+002,
  17. 1.95176276e+002, 1.00000000e-100]) ]
  18. Traceback (most recent call last):
  19. in <module>
  20. print("f 0 =",f[0])
  21. TypeError: '_io.TextIOWrapper' object is not subscriptable

The expected output is

  1. f read = [array([ 4.24805745e+002, 0.00000000e+000, 0.00000000e+000,
  2. 4.02753575e+002, 3.79606698e+002, 0.00000000e+000,
  3. 3.01629825e+002, 2.73930748e+002, 2.73930748e+002,
  4. 1.44270041e-014, -1.19840421e-014, 2.48466223e+002,
  5. 4.07808390e-014, 2.23084563e+002, 2.23084563e+002,
  6. 1.95133833e+002, -1.90524280e-014, 1.13675881e+002,
  7. 1.60355972e-023, 8.87020057e+001, 6.51230922e+001,
  8. 8.87020057e+001, 1.00000000e-100])
  9. array([ 4.24805745e+002, 0.00000000e+000, 0.00000000e+000,
  10. 3.96370317e+002, 3.82498020e+002, 0.00000000e+000,
  11. 3.77200451e+002, 3.22917850e+002, 3.49391875e+002,
  12. 3.24649000e+002, 3.06639099e+002, 3.06639099e+002,
  13. 3.06639099e+002, 2.74439993e+002, 3.06639099e+002,
  14. 2.42357202e+002, 2.42357202e+002, 2.26139235e+002,
  15. 3.06639099e+002, 2.22347429e+002, 1.64667856e+002,
  16. -1.26698275e-013, 1.69645075e+002, 1.07059051e+002,
  17. 1.95176276e+002, 1.00000000e-100]) ]
  18. f 0 = array([ 4.24805745e+002, 0.00000000e+000, 0.00000000e+000,
  19. 4.02753575e+002, 3.79606698e+002, 0.00000000e+000,
  20. 3.01629825e+002, 2.73930748e+002, 2.73930748e+002,
  21. 1.44270041e-014, -1.19840421e-014, 2.48466223e+002,
  22. 4.07808390e-014, 2.23084563e+002, 2.23084563e+002,
  23. 1.95133833e+002, -1.90524280e-014, 1.13675881e+002,
  24. 1.60355972e-023, 8.87020057e+001, 6.51230922e+001,
  25. 8.87020057e+001, 1.00000000e-100])
  26. </details>
  27. # 答案1
  28. **得分**: 0
  29. 假设你的`Test.txt`文件包含以下内容:
  30. ```text
  31. [array([ 4.24805745e+002, 0.00000000e+000, 0.00000000e+000,
  32. 4.02753575e+002, 3.79606698e+002, 0.00000000e+000,
  33. 3.01629825e+002, 2.73930748e+002, 2.73930748e+002,
  34. 1.44270041e-014, -1.19840421e-014, 2.48466223e+002,
  35. 4.07808390e-014, 2.23084563e+002, 2.23084563e+002,
  36. 1.95133833e+002, -1.90524280e-014, 1.13675881e+002,
  37. 1.60355972e-023, 8.87020057e+001, 6.51230922e+001,
  38. 8.87020057e+001, 1.00000000e-100])
  39. array([ 4.24805745e+002, 0.00000000e+000, 0.00000000e+000,
  40. 3.96370317e+002, 3.82498020e+002, 0.00000000e+000,
  41. 3.77200451e+002, 3.22917850e+002, 3.49391875e+002,
  42. 3.24649000e+002, 3.06639099e+002, 3.06639099e+002,
  43. 3.06639099e+002, 2.74439993e+002, 3.06639099e+002,
  44. 2.42357202e+002, 2.42357202e+002, 2.26139235e+002,
  45. 3.06639099e+002, 2.22347429e+002, 1.64667856e+002,
  46. -1.26698275e-013, 1.69645075e+002, 1.07059051e+002,
  47. 1.95176276e+002, 1.00000000e-100]) ]

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

  1. import numpy as np
  2. # 读取整个文件
  3. with open("Test.txt", "r") as f:
  4. inputdata = f.read()
  5. # 使用eval解析数据
  6. data = eval(
  7. inputdata
  8. .strip() # 去掉末尾的空白字符
  9. .replace("\n", "") # 用空字符串替换换行符
  10. .replace("),", "),\n") # 在数组之间添加逗号
  11. {"array": np.array} # 确保它知道将array解析为NumPy数组
  12. )
  13. print(data[0])

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

英文:

Assuming your Test.txt file contains:

  1. [array([ 4.24805745e+002, 0.00000000e+000, 0.00000000e+000,
  2. 4.02753575e+002, 3.79606698e+002, 0.00000000e+000,
  3. 3.01629825e+002, 2.73930748e+002, 2.73930748e+002,
  4. 1.44270041e-014, -1.19840421e-014, 2.48466223e+002,
  5. 4.07808390e-014, 2.23084563e+002, 2.23084563e+002,
  6. 1.95133833e+002, -1.90524280e-014, 1.13675881e+002,
  7. 1.60355972e-023, 8.87020057e+001, 6.51230922e+001,
  8. 8.87020057e+001, 1.00000000e-100])
  9. array([ 4.24805745e+002, 0.00000000e+000, 0.00000000e+000,
  10. 3.96370317e+002, 3.82498020e+002, 0.00000000e+000,
  11. 3.77200451e+002, 3.22917850e+002, 3.49391875e+002,
  12. 3.24649000e+002, 3.06639099e+002, 3.06639099e+002,
  13. 3.06639099e+002, 2.74439993e+002, 3.06639099e+002,
  14. 2.42357202e+002, 2.42357202e+002, 2.26139235e+002,
  15. 3.06639099e+002, 2.22347429e+002, 1.64667856e+002,
  16. -1.26698275e-013, 1.69645075e+002, 1.07059051e+002,
  17. 1.95176276e+002, 1.00000000e-100]) ]

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

  1. import numpy as np
  2. # read in the whole file
  3. with open(&quot;Test.txt&quot;, &quot;r&quot;) as f:
  4. inputdata = f.read()
  5. # parse the data with eval
  6. data = eval(
  7. inputdata
  8. .strip() # remove trailing whitespace
  9. .replace(&quot;\n&quot;, &quot;&quot;) # replace new lines with empty strings
  10. .replace(&quot;)&quot;, &quot;),&quot;), # stick a comma between the arrays
  11. {&quot;array&quot;: np.array} # make sure it knows to parse array as a NumPy array
  12. )
  13. print(data[0])
  14. array([ 4.24805745e+002, 0.00000000e+000, 0.00000000e+000,
  15. 4.02753575e+002, 3.79606698e+002, 0.00000000e+000,
  16. 3.01629825e+002, 2.73930748e+002, 2.73930748e+002,
  17. 1.44270041e-014, -1.19840421e-014, 2.48466223e+002,
  18. 4.07808390e-014, 2.23084563e+002, 2.23084563e+002,
  19. 1.95133833e+002, -1.90524280e-014, 1.13675881e+002,
  20. 1.60355972e-023, 8.87020057e+001, 6.51230922e+001,
  21. 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:

确定