Home > Enterprise >  Parse a Text file to a table using Python or SQL
Parse a Text file to a table using Python or SQL

Time:12-27

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

CodePudding user response:

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'}]
  • Related