-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.py
More file actions
137 lines (118 loc) · 4.79 KB
/
parse.py
File metadata and controls
137 lines (118 loc) · 4.79 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
##################################################################################################
# Authors: Natalie Eversole and Team members
# Date: 12/08/2022
# Description: This code parses the amazon-meta.txt file and outputs products.csv and reviews.csv
#################################################################################################
import pandas as pd
# Create lists for reviews and entries to put dictionaries in
entry_list = []
reviews = []
# Create empty dictionaries for each entry ID
with open('amazon-meta.txt', encoding='utf8') as f:
# List of ID's
ids = []
# Get every ID number from text file and add it the ID's list
for line in f:
if line.startswith('Id'):
id = line.strip().split(': ')[1]
ids.append(id)
# Create a dictionary for every ID in the ID's list
# The dictionary values will all be empty except for the ID itself
for id in ids:
dict = {}
dict['Id'] = id
dict['ASIN'] = ''
dict['title'] = ''
dict['group'] = ''
dict['salesrank'] = ''
dict['similar'] = ''
dict['categories'] = ''
dict['reviewTotal'] = ''
dict['downloaded'] = ''
dict['avgRating'] = ''
entry_list.append(dict)
f.close()
# Write values to dictionaries and add them to their respective list
with open('amazon-meta.txt', encoding='utf8') as f2:
# Set for categories to be added to
# This will prevent duplicates from being added
categories_set = set()
# Keep track of the current product ID
indexId = 0
# discontinued = 0
for line in f2:
# 5868 discontinued products
# if line.startswith(' discon'):
# discontinued += 1
# Get entry ID, update indexID and clear the categories_set
if line.startswith('Id'):
entryId = line.strip().split(': ')[1]
indexId = int(entryId)
categories_set.clear()
# Get ASIN
if line.startswith('ASIN'):
asin = line.strip().split(': ')[1]
entry_list[indexId]['ASIN'] = asin
# Get title
if line.startswith(' title'):
title = line.strip().split('title: ')[1]
entry_list[indexId]['title'] = title
# Get group
if line.startswith(' group'):
group = line.strip().split(': ')[1]
entry_list[indexId]['group'] = group
# Get salesrank
if line.startswith(' salesrank'):
srank = line.strip().split(': ')[1]
entry_list[indexId]['salesrank'] = srank
# Get entry reviews info
if line.startswith(' reviews:'):
reviewLine = line.strip().split()
total = reviewLine[2]
downloaded = reviewLine[4]
avgRating = reviewLine[7]
entry_list[indexId]['reviewTotal'] = total
entry_list[indexId]['downloaded'] = downloaded
entry_list[indexId]['avgRating'] = avgRating
# Get similar products
if line.startswith(' similar'):
similarLine = line.strip().split()
if similarLine[1] == '0':
entry_list[indexId]['similar'] = ''
else:
# Remove first two items in list
similarLine.pop(0)
similarLine.pop(0)
entry_list[indexId]['similar'] = ', '.join(str(elem) for elem in similarLine)
# Get categories
if line.startswith(' |'):
category_key_words = line.strip().split('|')
for item in category_key_words:
cat = item.split('[')
if cat[0] == '':
next
else:
categories_set.add(cat[0])
categories_string = ', '.join(categories_set)
entry_list[indexId]['categories'] = categories_string
# Get customer reviews info
if line.startswith(' 19') or line.startswith(' 20'):
custReview = line.strip().split()
reviewDict = {}
reviewDict['Id'] = str(indexId)
reviewDict['Customer'] = custReview[2]
reviewDict['Date'] = custReview[0]
reviewDict['Rating'] = custReview[4]
reviewDict['Votes'] = custReview[6]
reviewDict['Helpful'] = custReview[8]
reviews.append(reviewDict)
else:
next
f2.close()
# Convert lists of dictionaries to pandas dataframes and write them to CSV's
reviewsDF = pd.DataFrame(reviews)
reviewsDF = reviewsDF.astype(str)
entriesDF = pd.DataFrame(entry_list)
entriesDF = entriesDF.astype(str)
reviewsDF.to_csv('reviews.csv', index=False)
entriesDF.to_csv('products.csv', index=False)