在Python中将Amazon数据转换为CSV格式 [英] Converting amazon data into csv format in Python

查看:47
本文介绍了在Python中将Amazon数据转换为CSV格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有,我之前也问过类似的问题,但尚未解决.

all, I asked similar question before but haven't solved it yet.

我有一个Amazon Review数据集,并想将其转换为Python中的csv格式.我拥有的原始数据如下所示:

I have amazon review data set and would like to convert it into csv format in Python. The original data that I have look like as follows:

product/productId: B00032K32A
product/title: Cruiser Accessories 21330 Slim Rim, Chrome License Plate Frame
product/price: 4.99
review/userId: A2O41UFL8HAQWV
review/profileName: Nick Nefsik
review/helpfulness: 4/4
review/score: 5.0
review/time: 1239667200
review/summary: It's slim, alright!
review/text: Similar to another review, I also found that this frame is more of a overlay to a   license plate (sits on top of the plate), as opposed to securing the plate underneath it, if that makes sense.It *just* covers the edges of my AZ plate, which is fine, but I sure wouldn't want it to be any smaller around its outside perimeter. I also ordered the chrome covers for the screws (Cruiser Accessories 82030 Screw Covers, Chrome) that I was already using, and, altogether, it looks great, and is exactly the look I was going for.

product/productId: B00032K32A
product/title: Cruiser Accessories 21330 Slim Rim, Chrome License Plate Frame
product/price: 4.99
review/userId: A3V7H58BH72AYT
review/profileName: Illustratedman
review/helpfulness: 6/7
review/score: 5.0
review/time: 1199145600
review/summary: Nice...
review/text: I first purchased these for my new 2008 Honda Accord EX-L to complement the chrome on the car and though they looked nice I eventually ordered the 20130 version of the Cruiser chrome frame for the wider border.

结果应如下所示:

product/productId, product/title, product/price, review/userId, review/profileName, review/helpfullness, review/score, review/time, review/summary, review/text
B00032K32A, Cruiser Accessories 21330 Slim Rim, Chrome License Plate Frame, 4.99, A2O41UFL8HAQWV, Nick Nefsik, 4/4, 5.0, 1239667200, It's slim, alright!, Similar to another review, I also found that this frame is more of a overlay to a   license plate (sits on top of the plate), as opposed to securing the plate underneath it, if that makes sense.It *just* covers the edges of my AZ plate, which is fine, but I sure wouldn't want it to be any smaller around its outside perimeter. I also ordered the chrome covers for the screws (Cruiser Accessories 82030 Screw Covers, Chrome) that I was already using, and, altogether, it looks great, and is exactly the look I was going for.
B00032K32A, Cruiser Accessories 21330 Slim Rim, Chrome License Plate Frame, 4.99, A3V7H58BH72AYT, Illustratedman, 6/7, 5.0, 1199145600, Nice..., I first purchased these for my new 2008 Honda Accord EX-L to complement the chrome on the car and though they looked nice I eventually ordered the 20130 version of the Cruiser chrome frame for the wider border.

我具有与上述相同格式的大量数据(超过300MB),因此想要写入而不是打印.

I have pretty big amount of data (more than 300MB) with the same format above so want to write it instead of printing it.

我是python的新手,尝试了几种不同的方法,但还没有成功.是否有人想过将原始数据类型转换为csv格式?

I am a newbee to python and tried several different ways but still haven't succeeded it yet. Are there anyone who has a good thought about converting the original data type into csv format?

推荐答案

这是一个简单的解决方案.创建一个类来表示数据文件中的记录.然后遍历数据文件的每一行,将每一行映射到Record对象的属性.然后在对象上调用方法以将记录格式化为所需的CSV格式.

Here's a simple solution. Create a class to represent the records in your data file. Then iterate over each line of the data file mapping each line to a property on your Record object. Then call a method on the object to format the record into the CSV format you want.

import string
import sys

file = open('amazon.txt')
csv = ''

class Record:
    def toCSV(self):
        return self.productId + ',' + self.title


record = Record()
for line in file:
    if '\n' == line:
        csv += record.toCSV()
    elif "productId" in line:
        record.productId = line
    elif "title" in line:
        record.title = line
    #add conditions for other fields in the datafile

file.close()

print csv

这篇关于在Python中将Amazon数据转换为CSV格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆