3 ms·
User input of code. I use this in my Python data processing scripts. That is, when I generate data, I generate it as Python literals, either lists or dictionar
by scott_s 15y ago
User input of code.
I use this in my Python data processing scripts. That is, when I generate data, I generate it as Python literals, either lists or dictionaries. When I have to process it, minimal parsing needed, I just eval it into my code.
def parse_and_append(line, seq, str):
if str in line:
seq.append(eval(line[line.find(str) + len(str):]))
nums = []
mappings = {}
for line in data_file:
parse_and_append(line, nums, "nums: ")
parse_and_append(line, mappings, "mappings: ")
Terribly insecure for webpages, sure, but very efficient use of my time.
- eevee 15y agoJSON will let you do that just as well. Or, if you must, there's ast.literal_eval which will only allow Python literals.
- scott_s 15y agoI see no reason to use JavaScript to process my data files - keep in mind this is data post-processing of experiments, not a user-facing application. And I enjoy Python, so I'll stick with it. But thanks for the pointer to literal_eval, I have not explored that part of the standard library.