1

I think I know of a way to do this, but I'd like to see if there is a bettery way. So here is the the file for just one part I'm trying to read from (and then write back to)

['Part assembly name', <name of assembled part>]
['Parts', <part1>, <part2>, <part3>]
['Quantities', 5, 8, 2]
['Part category, <category type 1>, <category type 2>]

If I save each line to an array, I can write each one with json

myfile = file('partsList.txt', 'w')
for x in (names, ingedients, volumes, category):
    json.dump(x, myfile)
    myfile.write('\n')

I should able to read each line back with:

with open(fname) as f:
    names.append(f.readlines())
    parts.append(f.readlines())
    quanties.append(f.readlines())
    categories.append(f.readlines())

So after reading all of my different part assemblies from my file I should have four arrays (or maybe 1 2 dimensional array)

names = [<name of assembly 1>, <name of assembly 2>, <name of assembly 3>]
parts = [<array of parts for 1>, <array of parts for 2>, <array of parts for 3>]
quantites = [<array of quantites  for 1>, <array of quantites  for 2>, <array of quantites for 3>]
categories= [<array of categoriesfor 1>, <array of categoriesfor 2>, <array of categoriesfor 3>]

is there a better/easier way to do this? I don't want to try to reinvent the wheel. Thanks!

Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
gerrgheiser
  • 215
  • 1
  • 8
  • To me this is always the critical part of solving problems - the structure for your data. As @Martijun suggested - use a dictionary - you might want a dictionary of dictionaries the key for the outer dictionary is the name of the assembly and the value has as keys the parts, quantities etc and the values are those things. – PyNEwbie Aug 01 '13 at 17:34
  • good call on the dictionary of dictionaries. I think that will work out well. Thanks a bunch! – gerrgheiser Aug 01 '13 at 21:14

1 Answers1

1

Use a dictionary, then encode and write once, read and decode once:

with file('partsList.txt', 'w') as myfile:
    data = {'names': names, 'ingredients': ingredients, 'volumes': volumes, 'category': category}
    json.dump(data, myfile)

Reading:

with file('partsList.txt') as myfile:
    data = json.load(myfile)

names = data['names']
# etc.

or better still, start with the dictionary instead of separate variables in the first place.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • i did as suggested as used a dictionary to begin with. Question though, is there a way to make the file that i dump the data to read able for the multiple different part sets? As it is now, everything is just on one line. I tried to do a pretty print to the file, but i don't think i'm using it correctly. As @PyNEwbie suggested, i'm using a dictionary of dictionaries. – gerrgheiser Aug 01 '13 at 21:16
  • @gerrgheiser: By default `json.dump()` uses a compact format without newlines. See the indent and separator keyword arguments [in the documentation](http://docs.python.org/2/library/json.html#json.dump) to make it output something a little more readable. – Martijn Pieters Aug 01 '13 at 21:22
  • @gerrgheiser: However, if you leave it as one JSON object per line, you can write multiple entries (with newlines in between) and then read that data again, line per line, into new JSON objects. See [Loading & Parsing JSON file in python](http://stackoverflow.com/a/12451465) for a technique to read them again. – Martijn Pieters Aug 01 '13 at 21:24