vojtam
vojtam

Reputation: 1255

How to import list stored as txt into pandas?

I have a following problem. I need to load txt file as pandas dataframe. See, how my txt looks like:

[        {
            "type": "VALUE",
            "value": 93.5,
            "from": "2020-12-01T00:00:00.000+01",
            "to": "2020-12-01T00:00:01.000+01"
        },
        {
            "type": "VALUE",
            "value": 75,
            "from": "2020-12-01T00:00:01.000+01",
            "to": "2020-12-01T00:05:01.000+01"
        },
        {
            "type": "WARNING",
            "from": "2020-12-01T00:00:01.000+01",
            "to": "2020-12-01T00:05:01.000+01"
        } ]

In other words, I need to read the txt as a list of dictionaries. And then read it as a pandas dataframe. Desired output is:

      type       value    from                             to
0     VALUE      93.5     2020-12-01T00:00:00.000+01       2020-12-01T00:00:01.000+01
1     VALUE      75       2020-12-01T00:00:01.000+01       2020-12-01T00:05:01.000+01
2     WARNING    NaN      2020-12-01T00:00:01.000+01       2020-12-01T00:05:01.000+01

How can I do this, please? I look at this question, but it wasn`t helpful: Pandas how to import a txt file as a list , How to read a file line-by-line into a list?

Upvotes: 2

Views: 67

Answers (2)

jezrael
jezrael

Reputation: 863056

Use read_json, because if extension is txt still it is json file:

df = pd.read_json('file.txt')
    
print (df)
      type  value                        from                          to
0    VALUE   93.5  2020-12-01T00:00:00.000+01  2020-12-01T00:00:01.000+01
1    VALUE   75.0  2020-12-01T00:00:01.000+01  2020-12-01T00:05:01.000+01
2  WARNING    NaN  2020-12-01T00:00:01.000+01  2020-12-01T00:05:01.000+01

Upvotes: 2

Nk03
Nk03

Reputation: 14949

import json
import pandas as pd

with open('inp_file.txt', 'r') as f:
    content = json.load(f)

df = pd.DataFrame(content)

Upvotes: 3

Related Questions