-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMetaDataReaderProcess.py
More file actions
50 lines (37 loc) · 1.39 KB
/
MetaDataReaderProcess.py
File metadata and controls
50 lines (37 loc) · 1.39 KB
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
44
45
46
47
48
49
50
import pandas as pd
import os
import gzip
import demjson
import sys
sys.path.append('./')
from Constants import TPS_DIR, META_DIR, CATEGORY
def meta_file_reader(TP_file):
f= gzip.open(TP_file, 'r')
items_id, categories, description, title =[], [], [], []
for line in f:
js = demjson.decode(line)
if str(js['asin']) == 'unknown':
continue
try:
title.append(str(js['title']))
except:
continue
try:
categories.append(js['categories'])
except:
categories.append('')
try:
description.append(js['description'])
except:
description.append('')
items_id.append(str(js['asin']))
meta_data = pd.DataFrame({ 'item_id': pd.Series(items_id),
'categories': pd.Series(categories),
'description': pd.Series(description),
'title': pd.Series(title)})[['item_id', 'categories', 'description', 'title']]
print('reading finished...')
return meta_data
if __name__ == "__main__":
TP_file = os.path.join(TPS_DIR, META_DIR)
meta_data = meta_file_reader(TP_file)
meta_data.to_csv(os.path.join(TPS_DIR, CATEGORY + '_infor.csv'), index=False, header = ['item_id', 'categories', 'description', 'title'])