-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonl2csv.py
43 lines (31 loc) · 1013 Bytes
/
jsonl2csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Usage: python telegram-csv.py <path to json file> <path to output csv file>
# Example: python telegram-csv.py Bob.json Bob.csv
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()