Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. import csv
  2.  
  3. filepath = 'amazon-meta.txt'
  4. id = 1
  5. limit = 15001
  6. csv_data = []
  7. with open(filepath, mode='r') as file:
  8. line = file.readline()
  9. while id < limit:
  10. if 'group' in line and 'Book' in line:
  11. while 'cutomer' not in line:
  12. line = file.readline()
  13. while 'cutomer' in line:
  14. data = line[line.find('cutomer:') + len('cutomer:'):line.rfind('votes')].strip().replace('rating:', '').replace(' ', '', 2).replace(' ', ',', 1)
  15. data_as_list = data.split(',')
  16. csv_data.append([str(id), data_as_list[0], data_as_list[1]])
  17. line = file.readline()
  18. id += 1
  19. line = file.readline()
  20.  
  21. with open('books.csv', mode='w', newline='') as file:
  22. writer = csv.writer(file)
  23. writer.writerows(csv_data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement