我正在编写一个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>
__str__
实现。 - juanpa.arrivillagapandas
来写一个 csv 文件?这似乎有些过度了。你应该使用csv
模块。 - juanpa.arrivillagadf = pd.DataFrame(data)
更改为df = pd.DataFrame([obj.__dict__ for obj in data])
。Pandas 数据框需要从 Pandas 可以理解的对象构建,其中一个选项是字典列表。 - calico_