Files& eval() alternatives.. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Files& eval() alternatives..

okay.. so I have a file which contains [1,2,3] and '

#x27; and {a:1,b:2}. #list,sting character and dictionary So when I write it to a file using the write operation we know that the file always both writes and reads the content only as a String (pls correct if wrong).. So when I read them such as obj=f.read().. I get the content as obj='[1,2,3]' and '
#x27; and "{a:1,b:2}" Since they are all strings. I'll have to convert them into their original types... One nice way is to use eval() function.. So it's as follows for x in eval(obj): print x. #this prints the content... #OR parts=list (x for x in eval(obj))"""I don't know if it's correct pls correct and verify this for me pls(P.S: I'm a beginner.)""" #OR parts=[x for x in eval(obj)]# again corrections appreciated .... So my actual question is though eval() is very useful here.. it's known to run the content as 'code' and thus cause security concerns.... Is there a better way to extract the strings without knowing their types(from a file??)Thnx

3rd Aug 2018, 10:52 AM
Ultra Yam ZGH
Ultra Yam ZGH - avatar
7 Answers
+ 2
If you want the quick way, you can use json, pickle... If you decide to write to and read from a .txt-file, you could consider transforming the material each time when writing/reading. For example I have made a calory program for myself. It works with a list of tuples that look like this: ('cheese, lean', 261, 28, 0.1, 16) So there's a tuple with different types. I would write something like for (a, b, c, d, e) in foods: file.write(a+':')... then b c d e separated by spaces and a newline at the end. Result in the .txt: 'cheese, lean: 261 28 0.1 16\n' and so on for the other foods. When I read from that file again, I first use file.readlines() to split it by \n, then str.split(':') to get the food and its values separated, while protecting the spaces within the description. Then I'd split the right half with the values by space and float() each element in a loop. Finally, a, b, c, d, e get re-tupled and listed. That would be the step by step process - if you still want a manual transformation. ;-)
3rd Aug 2018, 3:02 PM
HonFu
HonFu - avatar
+ 1
If it's the data of the evaluated code that you're after (lists, dictionaries, etc), consider encoding this data in json format, and then decoding it with the standard json library in your python code. This will make the data dynamically available in your program, without the security issues.
3rd Aug 2018, 11:01 AM
Jeroen van der Laan
Jeroen van der Laan - avatar
+ 1
@ Jeroen van der Laan thnx...okay.. though that's interesting I don't know JSON :(..Any other ways?? And I want the original type back.. because since files only return strings I cannot do list operations even though a list is in the file..(or hash operations for that matter)
3rd Aug 2018, 11:04 AM
Ultra Yam ZGH
Ultra Yam ZGH - avatar
+ 1
Ultra Yam ZGH Json is very easy to learn. And python's standard json library will do type conversion for you. import json l = json.load("[1, 2, 3]") variable l now holds a list containting values 1, 2 and 3. Note that json.load took a string argument (which could be obtained by reading a file)
3rd Aug 2018, 11:20 AM
Jeroen van der Laan
Jeroen van der Laan - avatar
+ 1
@ Jeroen van der Laan...I checked up the JSON library and it's not that hard;) So import json.. then json.dumps(whatever content,f) & json.load(f.read())#where f=open('s.txt',r+) Thnx for the tip!! But any other solutions will be greatly appreciated!!
3rd Aug 2018, 11:24 AM
Ultra Yam ZGH
Ultra Yam ZGH - avatar
+ 1
@ Jeron van der Laan yeah I just checked it out.. thank you again:)
3rd Aug 2018, 11:26 AM
Ultra Yam ZGH
Ultra Yam ZGH - avatar
+ 1
that's an interesting method @ HonFu...And pickle.. how do you use it and what are the methods for it??Thnx for the answer:)
3rd Aug 2018, 3:50 PM
Ultra Yam ZGH
Ultra Yam ZGH - avatar