-1

I have a text file like this

{u'Product_id': u'1234567', u'Product_name': u'Apple', u'Product_code': u'2.4.14'}
{u'Product_id': u'1234123', u'Product_name': u'Orange', u'Product_code': u'2.4.20'}

I have searched it on google but not know yet what kind of string is this, it's not json . How to parse it to table using Python or SQL specifically PL/SQL ? Desired table result have column and row like this:

Product_id  Product_name    Product_code 
1234567     Apple           2.4.14
1234123     Orange          2.4.20
azro
  • 53,056
  • 7
  • 34
  • 70
Tom Tom
  • 328
  • 4
  • 15
  • 1
    What kind of string ? That is python code, I don't get how you can have that in a text file – azro Dec 26 '22 at 11:26
  • @azro just a txt file – Tom Tom Dec 26 '22 at 11:26
  • 1
    Who created that file ? That is VERY bad idea to put python dict like that in a text file, this is nonsense – azro Dec 26 '22 at 11:27
  • @azro this is not a dict or anything related to Python, just a txt file and I want to convert it to table – Tom Tom Dec 26 '22 at 11:28
  • 2
    What you show is EXACTLY dicts with unicode string in it, that is **VALID** python code. So that isn't a machine-readable content, that isn't JSON or something, but code – azro Dec 26 '22 at 11:29
  • @azro so first I need to clear all the 'u' stuff so it look like python dict – Tom Tom Dec 26 '22 at 11:33
  • You don't , that just means unicode strings, that is still valid – azro Dec 26 '22 at 11:42

1 Answers1

1

First of all, I must say that this method is not very healthy. In order for the code I wrote to work, the schema of the data in the txt should not change;

import ast

info = open('sample_file.txt')
exported = []
for row in info.readlines():
    exported.append(ast.literal_eval(row))
print(exported)
# output: 
# [{'Product_code': '2.4.14', 'Product_id': '1234567', 'Product_name': 'Apple'},
#  {'Product_code': '2.4.20', 'Product_id': '1234123', 'Product_name': 'Orange'}]
Sezer BOZKIR
  • 534
  • 2
  • 13
  • 1
    then Convert `list of dict` to Dataframe: https://stackoverflow.com/questions/20638006/convert-list-of-dictionaries-to-a-pandas-dataframe – Tom Tom Dec 26 '22 at 16:56