将对象列表写入csv文件

时间:2017-06-28 21:51:56

标签: python python-2.7 csv praw

我正在编写一个python程序,它循环遍历reddit提交,提取数据,并将其作为对象存储在列表中。但是我在将该列表写入csv文件时遇到问题。该文件已创建,但它只是为对象提供了某种id标记。我该如何更改csv代码?

代码

import praw
from datetime import datetime
import pandas as pd

class Submission:
    def __init__(self, time, score, title, text, ofReddit, serious):
        self.time = time
        self.score = score
        self.title = title
        self.text = text
        self.ofReddit = ofReddit
        self.serious = serious
data = []

reddit = praw.Reddit(client_id=id, client_secret=secret,
                     user_agent='testscript by /u/SilentButtDeadlies')
subreddit = reddit.subreddit('AskReddit')
for submission in subreddit.new(limit=50):
    time = datetime.utcfromtimestamp(submission.created_utc).hour
    score = submission.score
    title = len(submission.title)
    text = len(submission.selftext)
    if 'of reddit' in submission.title.lower():
        ofReddit = 1
    else:
        ofReddit = 0
    if '[serious]' in submission.title.lower():
        serious = 1
    else:
        serious = 0
    data.append(Submission(time, score, title, text, ofReddit, serious))
df = pd.DataFrame(data)
filename = 'AskRedditData' + str(datetime.now()) + '.csv'
df.to_csv(filename, index=False, encoding='utf-8')

CSV文件

0
<__main__.Submission instance at 0x1118f6ef0>
<__main__.Submission instance at 0x1118f68c0>
<__main__.Submission instance at 0x1118f6950>
<__main__.Submission instance at 0x1118c3758>
<__main__.Submission instance at 0x11239c638>
<__main__.Submission instance at 0x11239c5f0>
<__main__.Submission instance at 0x112398908>
<__main__.Submission instance at 0x112398998>
<__main__.Submission instance at 0x112398878>
<__main__.Submission instance at 0x1123989e0>
<__main__.Submission instance at 0x112398c68>
<__main__.Submission instance at 0x11239fe18>
<__main__.Submission instance at 0x11239fe60>
<__main__.Submission instance at 0x11239fea8>
<__main__.Submission instance at 0x11239fef0>
<__main__.Submission instance at 0x11239ff38>
<__main__.Submission instance at 0x11239ff80>
<__main__.Submission instance at 0x11239ffc8>
<__main__.Submission instance at 0x112404050>
<__main__.Submission instance at 0x112404098>
<__main__.Submission instance at 0x1124040e0>
<__main__.Submission instance at 0x112404128>
<__main__.Submission instance at 0x112404170>
<__main__.Submission instance at 0x1124041b8>
<__main__.Submission instance at 0x112404200>
<__main__.Submission instance at 0x112404248>
<__main__.Submission instance at 0x112404290>
<__main__.Submission instance at 0x1124042d8>
<__main__.Submission instance at 0x112404320>
<__main__.Submission instance at 0x112404368>
<__main__.Submission instance at 0x1124043b0>
<__main__.Submission instance at 0x1124043f8>
<__main__.Submission instance at 0x112404440>
<__main__.Submission instance at 0x112404488>
<__main__.Submission instance at 0x1124044d0>
<__main__.Submission instance at 0x112404518>
<__main__.Submission instance at 0x112404560>
<__main__.Submission instance at 0x1124045a8>
<__main__.Submission instance at 0x1124045f0>
<__main__.Submission instance at 0x112404638>
<__main__.Submission instance at 0x112404680>
<__main__.Submission instance at 0x1124046c8>
<__main__.Submission instance at 0x112404710>
<__main__.Submission instance at 0x112404758>
<__main__.Submission instance at 0x1124047a0>
<__main__.Submission instance at 0x1124047e8>
<__main__.Submission instance at 0x112404830>
<__main__.Submission instance at 0x112404878>
<__main__.Submission instance at 0x1124048c0>
<__main__.Submission instance at 0x112404908>

1 个答案:

答案 0 :(得分:2)

您的提交类似乎只是作为记录类型。您可能只需使用namedtuple。所以用以下代码替换你的类定义:

from collections import namedtuple
Submission = namedtuple('Submission', ['time', 'score', 'title', 'text', 'ofReddit', 'serious'])

现在你的其余代码应该可以工作了。 pandas并不知道如何解释您最初撰写的Submission课程。因此,它只生成一列Submission个对象,当它写入时,它会使用默认为str(Submission()) object的{​​{1}},因为您没有定义另一个__str__ __str__ 1}}。真的,你想要使用一个序列。 namedtuple函数实际上是一个类工厂,它创建了一个从tuple派生的记录类型,因此它具有非常方便的构造函数所需的所有便利功能。

现在,既然你使用的是Python 2,那么我就没有必要改变你对pandas的使用,即使它只是用来编写csv似乎有些过分。话虽这么说,让Python 2 csv模块与unicode玩得很好是一种痛苦,所以你不妨保持它。如果您可以切换到 Python 3 ,则只需将pandas内容替换为:

import csv
with open(filename, 'w', newline='', encoding='utf8') as f:
    writer = csv.writer(f)
    writer.writerow(Submission._fields) # we are being naughty here and using a private attribute
    writer.writerows(data)