0

I'm creating a simple script for blender and i need a little help with get some data from file i've created before via python.

That file got structure like below:

name first morph
values -1.0000 1.0000
data 35 0.026703 0.115768 -0.068769
data 36 -0.049349 0.015188 -0.029470
data 37 -0.042880 -0.045805 -0.039931
data 38 0.000000 0.115775 -0.068780
name second morph
values -0.6000 1.2000
data 03 0.037259 -0.046251 -0.020062
data 04 -0.010330 -0.046106 -0.019890
…

etc more 2k lines ;p

What i need is to create a loop that read for me those data line by line and put values into three different arrays: names[] values[] and data[] depending on first word of file line.

Manualy it should be like that:
names.append('first morph')
values.append( (-1.0000,1.0000))
data.append((35, 0.026703, 0.115768, -0.068769))
data.append((36, -0.049349, 0.015188, -0.029470))
data.append((37, -0.042880, -0.045805, -0.039931))
data.append((38, 0.000000, 0.115775, -0.068780))
names.append('second morph')
values.append( (-0.6000,1.2000))
…

I dont know why my atempts of creating that kind of 'for line in file:' loop creating more errors than complete data, i dont know why is going out of range, or not getting proper data

Please help how to automate that process instead of writing manualy each line since i already exported needed parameters into a file.

0

2 Answers 2

2
names = []
values = []
data = []
with open('yourfile') as lines:
    for line in lines:
        first, rest = line.split(' ', 1)

        if first == 'name':
            names.append(rest)

        elif first == 'values':
            floats = map(float, rest.split())
            values.append(tuple(floats))

        elif first == 'data':
            int_str, floats_str = rest.split(' ', 1)
            floats = map(float, floats_str.split())
            data.append( (int(int_str),) + tuple(floats) )

Why do you need it like this? How will you know where the next name starts in your data and values lists?

Sign up to request clarification or add additional context in comments.

2 Comments

I'll make a connection between name and value array index. For data array i'll add maybe one more parameter equvialent to current index in name array which will increase each time a 'name' will occur in file.
Are you sure you don't want a list of morph objects (or just tuples), with name, values, and data attributes?
0

Here is a simple python "for line in" solution... you can just call processed.py...

fp = open("data1.txt", "r")

data = fp.readlines()
fp1 = open("processed.py", "w")

fp1.write("names = []\nvalues=[]\ndata=[]\n")

for line in data:
    s = ""
    if "name" in line:
        s = "names.append('" + line[5:].strip() + "')"
    elif "values" in line:
        s = "values.append((" + ", ".join(line[7:].strip().split(" ")) + "))"
    elif "data" in line:
        s = "data.append((" + ", ".join(line[5:].strip().split(" ")) + "))"

    fp1.write(s + "\n");

fp1.close()
fp.close()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.