python 将JSONL文件从https://github.com/tvdstaaij/telegram-history-dump转换为CSV

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 将JSONL文件从https://github.com/tvdstaaij/telegram-history-dump转换为CSV相关的知识,希望对你有一定的参考价值。

# Converts a JSONL file generated with telegram-history-dump (1) to CSV
# Usage: python telegram-csv.py <path to json file> <path to output csv file>
# Example: python telegram-csv.py Bob.json Bob.csv
# 1: https://github.com/tvdstaaij/telegram-history-dump
from datetime import datetime
import unicodecsv as csv
import json, sys

def get_isodate(msg):
    date = msg.get("date", None)

    if not date:
        return "unknown"

    return datetime.fromtimestamp(date).isoformat()

def main():
    if len(sys.argv) != 3:
        sys.exit("No json and/or csv file given")

    jsonpath = sys.argv[1]
    csvpath = sys.argv[2]

    jsonfile = open(jsonpath, "r")
    csvfile = open(csvpath, "w")
    csvwriter = csv.writer(csvfile)

    csvwriter.writerow(["from", "to", "date", "text"])

    for item in jsonfile:
        msg = json.loads(item)

        csvwriter.writerow([
            msg["from"].get("print_name", "unknown"),
            msg["to"].get("print_name", "unknown"),
            get_isodate(msg),
            msg.get("text", "no text")
        ])

    jsonfile.close()
    csvfile.close()

if __name__ == "__main__":
    main()

以上是关于python 将JSONL文件从https://github.com/tvdstaaij/telegram-history-dump转换为CSV的主要内容,如果未能解决你的问题,请参考以下文章

无法从python中的json数组获取嵌套对象

有没有办法将 json 对象转换为 json l 文件

批量PDB转一个jsonl

批量PDB转一个jsonl

获取文件最后修改日期和文件名 pyspark 的脚本

如何将抓取的数据从 Scrapy 以 csv 或 json 格式上传到 Amazon S3?