2014-04-28 3 views
0

으로 모두 csv 형식으로 변환하기 전에 비슷한 질문을했지만 아직 해결하지 못했습니다.아마존 데이터를 파이썬

아마존의 리뷰 데이터 세트가 있고 파이썬에서 CSV 형식으로 변환하고 싶습니다. 나는 다음과 같이 가지고있는 원래 데이터 :

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. 

내가 그래서 위와 같은 형식의 데이터의 꽤 큰 금액을 (더 3백메가바이트 이상)이 그것을 인쇄하는 대신에 쓰려고합니다.

나는 파이썬에 대한 새비지로 여러 가지 방법을 시도했지만 아직 성공하지 못했습니다. 원래 데이터 형식을 CSV 형식으로 변환하는 것에 대해 좋은 생각을 가진 사람이 있습니까?

+0

빠른 참고 : 데이터에는 쉼표가 포함되어 있습니다. 따라서 데이터 세트를 쉼표로 구분 된 파일로 변환하면 표제가 정렬되지 않습니다. 데이터의 쉼표를 '-'또는 다른 것과 같이 다른 것으로 변환해야합니다. 이를 위해'replace' 메소드를 사용하십시오. – ssm

답변

0

귀하의 질문에 주어진 해결책 here이 왜 효과가 있었는지 확신 할 수 없지만 여기에는 같은 유형의 코드에 대한 다른 예가 나와 있습니다. 사용자의 목적에 맞게 INPUT_FILE_NAMEOUTPUT_FILE_NAME을 변경해야합니다.

INPUT_FILE_NAME = "Input.txt" 
OUTPUT_FILE_NAME = "Output.csv" 

header = [ 
    "product/productId", 
    "product/title", 
    "product/price", 
    "review/userId", 
    "review/profileName", 
    "review/helpfulness", 
    "review/score", 
    "review/time", 
    "review/summary", 
    "review/text"] 

f = open(INPUT_FILE_NAME) 
outfile = open(OUTPUT_FILE_NAME,"w") 

# Write header 
outfile.write(",".join(header) + "\n") 

currentLine = [] 
for line in f: 
    line = line.strip() 
    if line == "": 
     outfile.write(",".join(currentLine)) 
     outfile.write("\n") 
     currentLine = [] 
     continue 
    parts = line.split(":",1) 
    currentLine.append(parts[1]) 

if currentLine != []: 
    outfile.write(",".join(currentLine)) 


f.close() 
outfile.close() 
0

다음은 간단한 해결책입니다. 데이터 파일의 레코드를 나타내는 클래스를 만듭니다. 그런 다음 각 행을 Record 객체의 속성에 매핑하는 데이터 파일의 각 행을 반복합니다. 그런 다음 개체의 메서드를 호출하여 레코드를 원하는 CSV 형식으로 서식을 지정합니다.

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 
관련 문제