python 数组错误
你好,我正在尝试从文本文件加载坐标进行绘图,但我不断收到我不明白的错误。文件 中的坐标如下所示(0.1, 0.0, 0.0), (0.613125, 0.52202, 0.19919)
这是我尝试运行的代码:
from visual import *
with open ('/Desktop/Coordlist2.txt','r') as open_file:
rightFace = curve(pos=[(1,-1,-1), (1,-1,1), (1,-1,-1),(1,1,-1),(1,1,-1),(1,1,1),(1,1,1),(1,-1,1)], radius=0.01, color=color.cyan)
backFace = curve(pos=[(1,-1,-1), (-1,-1,-1), (-1,-1,-1),(-1,1,-1),(-1,1,-1),(1,1,-1)], radius=0.01, color=color.cyan)
leftFace = curve(pos=[(-1,-1,-1), (-1,-1,1), (-1,-1,1),(-1,1,1),(-1,1,1),(-1,1,-1)], radius=0.01, color=color.cyan)
frontFace = curve(pos=[(-1,-1,1), (1,-1,1), (1,1,1),(-1,1,1)], radius=0.01, color=color.cyan)
for line in open_file.readlines():
coords = line
points(pos=[coords], size=1, color=color.yellow)
这是我收到的错误消息:
Traceback (most recent call last):
File "/Users/Graphs.py", line 15, in <module>
points(pos=[coords], size=1, color=color.yellow)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/vis/primitives.py", line 84, in __init__
self.process_init_args_from_keyword_dictionary( keywords )
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/vis/primitives.py", line 212, in process_init_args_from_keyword_dictionary
setattr(self, key, value)
ValueError: Object cannot be converted to array.
任何帮助将不胜感激
hello I am trying to load coordinates for plotting from a text file and I keep getting an error I don't understand. The coordinates look like this in the file (0.1, 0.0, 0.0),
Here is the code I am trying to run:
(0.613125, 0.52202, 0.19919)
from visual import *
with open ('/Desktop/Coordlist2.txt','r') as open_file:
rightFace = curve(pos=[(1,-1,-1), (1,-1,1), (1,-1,-1),(1,1,-1),(1,1,-1),(1,1,1),(1,1,1),(1,-1,1)], radius=0.01, color=color.cyan)
backFace = curve(pos=[(1,-1,-1), (-1,-1,-1), (-1,-1,-1),(-1,1,-1),(-1,1,-1),(1,1,-1)], radius=0.01, color=color.cyan)
leftFace = curve(pos=[(-1,-1,-1), (-1,-1,1), (-1,-1,1),(-1,1,1),(-1,1,1),(-1,1,-1)], radius=0.01, color=color.cyan)
frontFace = curve(pos=[(-1,-1,1), (1,-1,1), (1,1,1),(-1,1,1)], radius=0.01, color=color.cyan)
for line in open_file.readlines():
coords = line
points(pos=[coords], size=1, color=color.yellow)
This is the error message I am getting:
Traceback (most recent call last):
File "/Users/Graphs.py", line 15, in <module>
points(pos=[coords], size=1, color=color.yellow)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/vis/primitives.py", line 84, in __init__
self.process_init_args_from_keyword_dictionary( keywords )
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/vis/primitives.py", line 212, in process_init_args_from_keyword_dictionary
setattr(self, key, value)
ValueError: Object cannot be converted to array.
Any help would be greatly appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它们在文件中的外观无关紧要;它们被读取为字符串。您需要先解析这些行,然后才能使用它们;尝试
ast.literal_eval()
。How they look in the file is irrelevant; they're read as strings. You'll need to parse the lines before they can be used; try
ast.literal_eval()
.问题就在这里:
当你从文件中读取一行时,你总是得到一个字符串。然后,您必须处理该字符串以生成您需要的任何数据结构。因此,如果您有一条看起来像这样的行(例如),
您必须显式地将其分解并从中创建一个元组:
另外,正如 agf 提醒我的那样,您可能应该只执行
for line in open_file
;open_file.readlines
在内存中创建文件的副本,而for line in open_file
只是单独迭代各行,而不将整个文件复制到内存中。为了尽可能完整,要转换如下所示的字符串:
您可以这样做:
只要每行始终有两个 3 的元组,此方法就有效。
agf 在他的评论中的解决方案也有效,但它有点脆弱,因为元组必须完全由
'), ('
分隔。说实话,Ignacio 的解决方案真的是最好的:)The problem is here:
When you read a line from a file, you always get a string. You then have to process that string to produce whatever data structure you need. So if you have a line that looks like this (for example)
you have to explicitly break it up and create a tuple from it:
Also, as agf reminded me, you should probably just do
for line in open_file
;open_file.readlines
creates a copy of the file in memory, whilefor line in open_file
just iterates over the lines individually, without copying the entire file into memory.Just to be as complete as possible, to convert a string that looks like this:
You can do this:
This works as long as there are always two tuples of 3 per line.
agf's solution in his comment works too, but it's a bit more brittle, since the tuples must be separated by
'), ('
exactly. To tell the truth, Ignacio's solution is really the best. :)