简体   繁体   English

Pymel:如何从复杂的.TXT文件中提取这些矢量浮点数?

[英]Pymel: How do I extract these vector floats from a complex .TXT file?

I am having trouble wrapping my head around how I would extract the float values from a complex text file in Pymel. 我无法解决如何从Pymel中的复杂文本文件中提取float值的问题。 I am not a programmer, I am an artist, however I am in need of creating a script for a specific workflow process and I have a beginner level knowledge of python. 我不是程序员,我是艺术家,但是我需要为特定的工作流程创建脚本,并且我对python具有初学者的知识。

My goal: to create objects in 3D space with (x,y,z) coordinates parsed from a specific text file from another program. 我的目标是:使用从另一个程序的特定文本文件解析的(x,y,z)坐标在3D空间中创建对象。

Ex. 例如 of text file: 文本文件:

point 1 8.740349 -4.640922 103.950059 点1 8.740349 -4.640922 103.950059
point 2 8.520906 3.447561 116.580496 点2 8.520906 3.447561 116.580496
point 3 4.235010 -7.562914 99.632423 点3 4.235010 -7.562914 99.632423
etc., etc 等等

there's much more space in my text file between the point #'s and the vector floats. 在我的文本文件中,在#号点和矢量浮动点之间有更多空间。

I want to create a dictionary that I will use to create my objects in my 3D program. 我想创建一个字典,该字典将用于在3D程序中创建对象。

For example, 例如,

myDictionary = {(point 1),[8.740349,-4.640922,103.950059]), etc. }. myDictionary = {(point 1),[8.740349,-4.640922,103.950059])等}。

This is my code snippet so far: 到目前为止,这是我的代码段:

def createLocators():
global filePath 
global markerNum
global markerCoord  
print "getting farther! :)"
with open(filePath,'r') as readfile:
    for line in readfile:           
        if "point" in line:             
            Types = line.split('                                                            ')
            markerNum = [Type[1] for Type in Types]
            markerCoord = [Type[2] for Type in Types]
            print markerNum, markerCoord

As you can see in the code, the space between the information is long. 如您在代码中所见,信息之间的间隔很长。 I figure if I can remove that space I can get two data sets that will be easier to work with. 我认为,如果可以删除该空间,我将获得两个更易于使用的数据集。 There is also many more lines in the text document that I don't care about, hence the if statement to filter only lines that start with "point". 文本文档中还有许多我不关心的行,因此if语句仅过滤以“ point”开头的行。 When I run createLocator() to test to see if it's splitting up the lines into my two lists it runs fine, but the print looks empty to me. 当我运行createLocator()进行测试以查看是否将行拆分为我的两个列表时,它运行良好,但是打印对我来说似乎是空的。

ex. 例如

[' '] [' '] [''] ['']

I've tried googling and searching answers here on SO, and searching both Pymel and regular python documentation for what I'm doing wrong or better approaches, but I have to admit the extent of my knowledge ends here. 我曾尝试在此处搜索和搜索答案,然后在Pymel和常规python文档中搜索我在做的错误或更好的方法,但是我必须承认我的知识到这里为止。

What am I doing wrong? 我究竟做错了什么? Is there a better and more efficient way to extract the data I need that I'm missing? 是否有更好,更有效的方法来提取丢失的我需要的数据?

Thanks for reading! 谢谢阅读!

First, you probably do not want to be splitting on that massive string of spaces. 首先,您可能不希望在大量的空格上分裂。 In fact what you almost certainly want is to just use line.split() with no arguments, as this will split apart all text on any kind, and any amount, of whitespace. 实际上,您几乎可以肯定要使用的是不带参数的line.split() ,因为这会将所有文本分割成任何种类和任何数量的空格。 ie: 即:

>>> 'A B      C\t\n\t   D'.split()
['A', 'B', 'C', 'D']

Then, assuming the format you've shown is correct, you should need need to get Types[2:5] , ie the second, third, and fourth elements of Types , for the coordinates. 然后,假设你已经证明的格式是正确的,如果您需要需要得到Types[2:5]即第二,第三和第四元素Types ,为坐标。

Beyond this, you should not be using capital names for local variables, and you should not be using global variables. 除此之外,您不应该对局部变量使用大写名称,也不应对全局变量使用。 Use function arguments instead, and rename Types to split_line or something. 请改用函数参数,然后将Types重命名为split_line或其他名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM