2

I have a file which contains list of lists, its a text file, and it looks like that:

    [[ שומר
 ,קולורבי
 ,קיווי
 ,"תפו""ע פינק ליידי"
 ,גזר
 ,עגבניה
 ,Unknown
 ] ,
[ מארז נייצ'ר ואלי שיבולת שועל עם שבבי שוקולד
 ,פיטנס חטיפי פריכיות דקות קינמון 60 גרם
 ,פריכיות משולשות פלפל
 ,מארז נייצ'ר ואלי שיבולת שועל עם שבבי שוקולד
 ,Unknown
 ,בננה
 ,"תפו""ע פינק ליידי"
 ,"תפו""ע גרנד"
 ,מארז 5 חטיפי פרי דגנים תמר תפוח ללא סוכר
 ,Unknown
 ] ,
[ "תפו""א לבן ארוז דוד משה"
 ,כרוב לבן
 ,קישוא
 ,גזר
 ,בטטה
 ] ,
[ Unknown..........

I need to convert it to pandas DataFrame, like wide columnstore, just one list = one row. Any help would be amazing! I am working with Jupyter Notebook - Python 3.

2 Answers2

1

Read the file text and try below code -

import json
import pandas as pd

data = open('data.txt', 'r', encoding = 'windows-1255', errors='ignore').read().replace("\r","").replace("\n","")


remove_doulequotes = data.replace('""', '').replace('"', '')
list_of_str = list(map(lambda x: '"{x}"'.format(x=x), remove_doulequotes.split(",")))
final_data = ", ".join(list_of_str).replace('"[[', '[["').replace(']"','"]').replace(']]"', '"]]').replace('"[', '["').replace(']"]', '"]]')

data_in_list = json.loads(final_data)

df = pd.DataFrame(data_in_list)

Or You can use ast.literal_eval function also -

import ast

data = open('data.txt', 'r', encoding = 'windows-1255', errors='ignore').read().replace("\r","").replace("\n","")

remove_doulequotes = data.replace('""', '').replace('"', '')
list_of_str = list(map(lambda x: '"{x}"'.format(x=x), remove_doulequotes.split(",")))
final_data = ", ".join(list_of_str).replace('"[[', '[["').replace(']"','"]').replace(']]"', '"]]').replace('"[', '["').replace(']"]', '"]]')

data_in_list = ast.literal_eval(final_data)

df = pd.DataFrame(data_in_list)

Additional codes added for string manipulation to format string properly.

RockStar
  • 1,304
  • 2
  • 13
  • 35
0
import json
with open('txt.txt') as f:
    lst = json.load(f)

df = pd.DataFrame(lst)

Should give you what you want.

selib
  • 39
  • 2