My text file have content like this- {"Id":123, "Name":"xyz"} {"Id":124, "Name":"abc"} {"Id":125,"Name":"kaggle"}
My desired output- Id Name 123 xyz 124 abc 125 Kaggle
- change into pretty table
- change into Pandas DataFrame
from tableViewClass import table | |
file_name = "trans.txt" | |
data = table(file_name) | |
pd_table = data.pd() # return pandas table | |
pt_table = data.pt_table() #return Pretttytable | |
print(pd_table) | |
print(pt_table) |
pandas | |
prettytable |
class table: | |
"""Class table""" | |
def __init__(self , file_name): | |
import json | |
self.file_n = file_name | |
f = open(self.file_n , "r") | |
data = f.read().split("\n") | |
self.data = [json.loads(x) for x in data] | |
self.label = list(self.data[0].keys()) | |
def pd(self): | |
"""Return Pandas DataFrame""" | |
import pandas as pd | |
frame = pd.DataFrame(self.data,columns=self.label) | |
return frame | |
def pt_table(self): | |
"""Return Data in Pretty table""" | |
from prettytable import PrettyTable | |
table = PrettyTable() | |
table.field_names = self.label | |
for x in self.data: | |
table.add_row(x.values()) | |
return table |