python 数组错误

发布于 2024-11-26 03:50:08 字数 1459 浏览 1 评论 0原文

你好,我正在尝试从文本文件加载坐标进行绘图,但我不断收到我不明白的错误。文件 中的坐标如下所示(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),
(0.613125, 0.52202, 0.19919)
Here is the code I am trying to run:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月竹挽风 2024-12-03 03:50:08

它们在文件中的外观无关紧要;它们被读取为字符串。您需要先解析这些行,然后才能使用它们;尝试 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().

日暮斜阳 2024-12-03 03:50:08

问题就在这里:

for line in open_file.readlines():
    coords = line

当你从文件中读取一行时,你总是得到一个字符串。然后,您必须处理该字符串以生成您需要的任何数据结构。因此,如果您有一条看起来像这样的行(例如),

l = '(5, 6, 7)'

您必须显式地将其分解并从中创建一个元组:

l_tuple = tuple(int(n) for n in l.strip('()').split(','))

另外,正如 agf 提醒我的那样,您可能应该只执行 for line in open_file; open_file.readlines 在内存中创建文件的副本,而 for line in open_file 只是单独迭代各行,而不将整个文件复制到内存中。

为了尽可能完整,要转换如下所示的字符串:

s = '(0.1, 0.0, 0.0), (0.613125, 0.52202, 0.19919)'

您可以这样做:

>>> numbers = tuple(float(n.strip('( )')) for n in s.split(','))
>>> t1, t2 = numbers[:3], numbers[3:]

只要每行始终有两个 3 的元组,此方法就有效。

agf 在他的评论中的解决方案也有效,但它有点脆弱,因为元组必须完全由 '), (' 分隔。说实话,Ignacio 的解决方案真的是最好的:)

The problem is here:

for line in open_file.readlines():
    coords = line

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)

l = '(5, 6, 7)'

you have to explicitly break it up and create a tuple from it:

l_tuple = tuple(int(n) for n in l.strip('()').split(','))

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, while for 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:

s = '(0.1, 0.0, 0.0), (0.613125, 0.52202, 0.19919)'

You can do this:

>>> numbers = tuple(float(n.strip('( )')) for n in s.split(','))
>>> t1, t2 = numbers[:3], numbers[3:]

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. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文