Home > Software design >  Write bytes data into a data frame
Write bytes data into a data frame

Time:04-04

I scraped one site and I get the data in bytes and I do not know how I can write them into a DF in some efficient way.

Here is a piece of my data:

b'[{"ID":"1","A":"2021-08-04 11:00:00","B":"2021-08-05 10:52:30","C":"","D":"2021-08-05 10:51:00","E":"","F":"Mark","G":"","H":"BOSTON"}]'

CodePudding user response:

Your problem is not how to transform bytes into a df, but how to transform bytes into a data structure that is "friendly" to df.

"eval" will try to transform the string/bytes into a data structure; in this case: a list of dictionaries

pd.DataFrame(eval(a), columns=eval(a)[0].keys())

note: if the bytes are valid jsons, its more efficient to use "json.loads" instead of "eval"

  • Related