简体   繁体   中英

Read .txt file into pandas dataframe

I have a .txt file. The format is like this:

12:12 {"name": "alice", "id":"1", "password":"123"}

12:14 {"name": "bob", "id":"2", "password":"1fsdf3"}

12:18 {"name": "claire", "id":"3", "password":"12fs3"}

I want to convert it to a pandas dataframe. The columns would be [timestamp, name, id, password]. Each column would have the corresponding information. Any idea how to do it? Much appreciated!

Create the rows of the DataFrame by processing one line of the file at a time. Then, call the DataFrame constructor:

import pandas as pd
import json

rows = []
with open("data.txt") as input_file:
    for line in input_file:
        line = line.strip()
        if line:
            timestamp, blob = line.split(maxsplit=1)
            # Use dict(**json.loads(blob), timestamp=timestamp) for Python <3.9
            blob = json.loads(blob) | dict(timestamp=timestamp)
            rows.append(blob)

    result = pd.DataFrame(rows)
    print(result)

This outputs:

     name id password timestamp
0   alice  1      123     12:12
1     bob  2   1fsdf3     12:14
2  claire  3    12fs3     12:18

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM