Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Modify a variable from an external file.

Hello guys!

I was wondering how can I do this:

I have a data file and my python code, and I want the code to read the file and print out the keys of the dictionary and also to add elements if its needed, I was able to do this if the variable is IN the file but seems unpractical.

Here is the link to the project in github, feel free to use it if you want (BTW its in spanish if you want an english version just ask me out) https://github.com/tvukasovic/Book-Adminsitrator/blob/master/Book_Adm.py

4 Answers

Well I found one answer that was using pickle library

Yes, pickle is very useful for saving various python data structures. Did it solve your problem, or are you still looking for solutions?

Bryan if you have any other solution It would be swell, you can never have just one solution haha

The file you are working from, is it essentially a dictionary object then?

Yup just a simple dictionary

You might try looking at JSON for encoding complex data types. I haven't used it too much to encode data that I'm working with, but I have used it before to decode data I'm getting from various servers/data sources.

Yeah I thought on using JSON but as it is Python I fount it weird but Thats a good solution the format is almost the same and its easy to manipulate, thanks a lot Bryan

No problem at all!