问题描述
我正在编写一个程序,该程序从 CSV 文件中读取一系列 comment.id
,将新的 comment.id
附加到该系列,然后将新系列另存为 CSV。
这是我所拥有的:
def get_cache(path_=PATH_CACHE):
""""
If cache does not exist,it creates one. Otherwise,it loads the
cache into memory.
"""
print('Searching for cache...')
if os.path.exists(path_)==True:
print('Cache exists')
cache = pd.read_csv(path_)
print('Cache loaded')
else:
print('Cache does not exist')
cache = pd.Series(['test1','test2'],name='commentid',dtype=str)
print('Cache created')
return cache
def run(reddit,reply,cache,sub='test'):
"""
Posts on the specified subreddit if a comment containing a keyword
is found. Updates cache in memory.
"""
subreddit = reddit.subreddit(sub)
comments = subreddit.comments(limit=25)
print('Searching for comments...')
for comment in comments:
comment_text = comment.body
isMatch = any(string in comment_text for string in MATCH_WORDS)
if isMatch and comment.id not in cache.values:
print('Comment found!')
# comment.reply(reply)
print('Replied!')
cache = cache.append(pd.Series([comment.id],dtype=str),ignore_index=True)
print('{} added to cache'.format(comment.id))
else:
print('Skip')
print('Done Searching \nCache:\n {}'.format(cache))
return cache
def update_cache(cache,path_=PATH_CACHE):
"""
Saves cache as csv to same path as it was loaded in from,replacing
the csv file.
"""
cache.to_csv(path_,index=False)
print('Cache saved as csv')
当发现新的 comment.id
时,我会得到 cache
的以下信息:
其中 gshiiiq
是调用 comment.id
时找到的 run()
。我希望将 gshiiiq
附加到现有列的末尾,而不添加新列。
任何帮助将不胜感激。
解决方法
作为 AsishM。在他的评论中指出,pd.read_csv()
默认返回一个数据帧。我设置了 squeeze=True
,它返回一个系列。