问题描述:

I am having trouble wrapping my head around how I would extract the float values from a complex text file in Pymel. 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.

My goal: to create objects in 3D space with (x,y,z) coordinates parsed from a specific text file from another program.

Ex. of text file:

point 1 8.740349 -4.640922 103.950059

point 2 8.520906 3.447561 116.580496

point 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.

For example,

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

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". 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.

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.

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. i.e:

>>> '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], i.e. the second, third, and fourth elements of Types, for the coordinates.

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.

相关阅读:
Top