【问题标题】:How to parse jsonlines file using pandas如何使用熊猫解析 jsonlines 文件
【发布时间】:2019-04-29 02:14:43
【问题描述】:

我是 python 新手,正在尝试从包含数百万行的文件中解析数据。试图去老学校使用 excel 解析它,但它失败了。如何有效地解析信息并将其导出为ex​​cel文件,以便其他人阅读?

我尝试使用其他人提供的此代码,但到目前为止没有运气

import re
import pandas as pd

def clean_data(filename):
    with open(filename, "r") as inputfile:
        for row in inputfile:
            if re.match("\[", row) is None:
                yield row

with open(clean_file,  'w') as outputfile:
    for row in clean_data(filename):
        outputfile.write(row)
NameError: name 'clean_file' is not defined

【问题讨论】:

    标签: python json jsonlines


    【解决方案1】:

    看起来clean_file没有定义,这可能是复制/粘贴代码的问题。

    您的意思是写入一个名为“clean_file”的文件吗?在这种情况下,您需要将其括在引号中:with open("clean_file", 'w')

    如果你想使用 json,我建议查看 json package,它有很多用于加载和解析 json 的工具。否则,如果json是平的,你可以使用内置的pandas函数read_json

    【讨论】:

    • 是的,我需要定义clean_file,并在最后添加了max_records = 1e5,它可以工作。非常感谢对新手的帮助!
    猜你喜欢
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多