简体   繁体   English

使用 Python 将格式奇怪的文件转换为 csv

[英]Converting weirdly formatted file to csv using Python

I would like to convert a strangly formatted file into a csv using Python.我想使用 Python 将格式奇怪的文件转换为 csv。

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.无论如何,有几行像上面那样,为了正确分析数据,我需要先将其格式化为 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?我知道 pandas 可以转换为 csv,但首先它需要能够理解我的文件,对吗?

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.也许是一个奇怪的解决方案,它会在“类型”上引发错误,因为 b 未定义。 But if you know what types you're going to have, you can just define them, like b = "b" or with但是如果你知道你将拥有什么类型,你可以定义它们,比如b = "b"

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

Then do whatever writing you need to the csv.然后对 csv 进行任何您需要的写入操作。

I ended up using a JSON converter, as the file itself was so close to a JSON file anyway.我最终使用了 JSON 转换器,因为无论如何文件本身都非常接近 JSON 文件。

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.所以问题不是真正的格式,而是一个有问题的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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