简体   繁体   中英

Converting weirdly formatted file to csv using Python

I would like to convert a strangly formatted file into a csv using Python.

The file looks like this:

[
{"kind": 342, "type": b, "pattern": {"circle": ["Oo0"]}, "number": 5}
...
...
...
]

The Information itself makes sense in context. (I promise!)

Anyway, there are a couple hundert lines like the one above, and to properly analyse the data I need to first format it into a csv.

I would like the nested object to just be the value to the pattern.

I know pandas can convert to csv, but first it needs to be able to understand my file, right?

Anyhow, any help would be greatly appreciated!

Since you're just converting this once, this weird way may suffice:

exec('x.append({"kind": 342, "type": b, "pattern": {"circle": ["Oo0"]}, "number": 5})')

Perhaps a strange solution, and it throws an error on "type" because b is not defined. But if you know what types you're going to have, you can just define them, like b = "b" or with

for type in "abcd":
 exec(f'{type} = "{type}"').

Then do whatever writing you need to the csv.

I ended up using a JSON converter, as the file itself was so close to a JSON file anyway.

Most likely the suggestions in the comments would have worked, if not for an issue with some lines deep in the file. There the nested objects where formated wrong.

[..
{... "pattern":{"circle":["OO"]}, "direction":"west":[42]}
...]

Some {} where missing.

So the issue wasn't really the format, as much as it was a file crafted with issues.

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