Why don't you just store all the data you want into a dictionary and serialize
it with json or pickle?
import pickle
def write_data(data, path):
with open(path, 'w') as f:
pickle.dump(data, f)
def read_data(path):
with open(path, 'r') as f:
data = pickle.load(f)
return data
That way you end up with a python object and don't have to worry about string
parsing.
-----
As an aside, to select every-n item in a list, list slices have a step
parameter. List slice notation is list[start:stop:step], so range(10)[::2] will
return every other item (0, 2, 4, 6, 8). range(10)[::3] will return every third
item (0, 3, 6, 9). And so on.
Also don't use str.split(theLine). Strings have a split method: theLine.split()
will do the same thing.
And File objects are iterators, so instead of doing:
for line in myFileObject.readlines():
you can just do:
for line myFileObject:
On Thursday, May 23, 2013 8:49:09 AM UTC-7, Daz wrote:
> Heya
>
> So I'm playing with some ways of storing data and then loading it again. I
> got the part of saving it to HDD. But once I want to load it, I kind of run
> in some issues. Because all data I can save is being stored "linearly" so its
> just "matterial 1 material 1 material 1"
>
> now to load the file I go with this so far :
>
> import maya.cmds as mc
> myFileObject=open('C://data2.txt', 'r')
> theLines = myFileObject.readlines()
> count = 0
> for line in theLines:
> theLine = theLines[count]
> theSplit = str.split(theLine)
> theValueS = theSplit[0]
> theFrameS = theSplit[1]
> print theSplit
> print theValueS
> print theFrameS
>
> but where I type theSplit[0] it only (naturally) select the string that is in
> []. How can I select every second string? so 0-2-4-6-8, and then 1-3-5-7-9. I
> guess I need some math skill for this one.(I guess I should pay more
> attention in school)
>
> But on the other hand I'm not sure that is the best way to go. With sets of
> data of 2 that would work(where I got material + value) but if I got 2 values
> then I'm in trouble so maybe I should save the data to txt file with some
> index list so that I can pick 1-10 strings for names, 11-20 for values and so
> on...
>
> so I save index list of how many materials I have and then use that separate
> and pick data somehow humh...
>
> What do you guys think?
--
You received this message because you are subscribed to the Google Groups
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.