我有亚马逊评论数据集,并希望将其转换为Python中的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.
我有相当大的数据(超过300MB),上面的格式相同,所以想要写它而不是打印它。
我是python的新手并尝试了几种不同的方式,但仍然没有成功。是否有人考虑过将原始数据类型转换为csv格式?
答案 0 :(得分:0)
我不确定为什么为您的问题here提供的解决方案不起作用,但这是相同类型代码的另一个示例。请注意,您应更改INPUT_FILE_NAME
和OUTPUT_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()
答案 1 :(得分: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