There will come condition when you have MySQL data that have structure like tuples in Python. For example, given data in categories.txt:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | (‘Woodenware’, 2594), (‘Woodworking’, 2595), (‘Woodworking Equipment and Supplies’, 2596), (‘Word Processing Service’, 2597), (‘Work Clothing – Retail’, 2598), (‘Work Clothing – Whol and Mfrs’, 2599), (‘Wrecker and Towing Services’, 2600), (‘Wrecking Contractors’, 2601), (‘Writers’, 2602), (‘Writers Service’, 2603), (‘X-Ray Apparatus and Supplies’, 2604), (‘X-Ray Inspection Service’, 2605), (‘X-Ray Laboratories – Industrial’, 2606), (‘X-Ray Laboratories – Medical’, 2607), (‘X-Ray Protection’, 2608), (‘Yarn – Retail’, 2609), (‘Yoga Instruction’, 2610), (‘Yogurt – Retail Shops’, 2611), (‘Yogurt – Wholesale’, 2612), (‘Youth Organizations and Centers’, 2613), (‘Zoos’, 2614) |
We want to process this text file into python data with tuple format. Luckily, Python have useful module to convert string into Python called “ast” (Abstraction Syntax Tree). Ast provide function called “literal_eval” which it will detect string format and convert into python objects by looking on format structure. Here is a bit explanation from Python.org :
ast.literal_eval(node_or_string)
Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None.
This can be used for safely evaluating strings containing Python expressions from untrusted sources without the need to parse the values oneself.
Now, let said we want to process categories.txt and convert into python objects:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import ast def process(): f = open("popular.txt") result = [] with open("popular.txt") as f: data = f.readlines() for line in data: value = ast.literal_eval(line.rstrip(",n")) result.append(value) return result if __name__ == "__main__": result = process() print result |
You will receive result :
Now you can also convert another string into another python objects easily.
Update
There is alternative to doing this by:
1 | x = [tuple(i) for i in json.loads(‘[‘ + open(‘category.txt’, ‘rb’).read().replace("’",’"’).replace(‘(‘,'[‘).replace(‘)’,’]‘) + ‘]’)] |
Thanks dude! http://eugene-yeo.me/