-1

I have a txt file with repeating lines:

1.json
string 'x;y;z', hash 1234
2.json
string 'xx;yy;zz', hash 5678
3.json

and I really want to save it into dataframe with columns:

| json | string | hash |

As a result I would like a dataframe containing for example in the first row:

| 1.json |  'x;y;z' | 1234
Alessandro Togni
  • 680
  • 1
  • 9
  • 24
Forest
  • 1
  • 1
  • 1
    What is your question about this? What was the problem with your attempts to do it? – mkrieger1 Apr 13 '22 at 20:17
  • Looks like you need to write a small parser – mozway Apr 13 '22 at 20:32
  • I am iterating over lines in the file and I wanted to prepare three lists (json, string, hash and then prepare a data frame with them) but I have a problem with writing a condition for parsing the line with string 'x;y;z', hash 1234. – Forest Apr 13 '22 at 20:53

1 Answers1

1

You can create a Dataframe with the column names:

df = pd.DataFrame(columns=["json", "string", "hash"])

Then you can add the needed values to the three columns for example in this way:

list = """1.json
string 'x;y;z', hash 1234
2.json
string 'xx;yy;zz', hash 5678""".split("\n")
df["json"] = list[0::2]
df["string"] = [x.split(",")[0].replace(" string ","") for x in 
list[1::2]]
df["hash"] = [x.split(",")[1].replace(" hash ","") for x in list[1::2]]
fed
  • 164
  • 9