【问题标题】:comparing two csv files in Python moving data from on to the other比较 Python 中的两个 csv 文件,将数据从一个移到另一个
【发布时间】:2015-08-23 21:15:01
【问题描述】:

我正在尝试比较 2 个 csv 文件:

tweet6.csv 包含我所有的推文数据

tweetcheck.csv 将由 tweet6.csv 中的数据填充,以仔细检查此数据以查看是否已发布推文。脚本第一次运行时为空

基本上,我正在运行一个脚本来执行搜索并返回将被转发的推文 鉴于搜索 api 几乎总是会返回我已经转发的推文,并且我不想在每次运行它时手动调整脚本,我需要上面的 csv 文件来检查这一点,并且只转发尚未在 tweetcheck 中的推文.csv

这是我的一个非常有缺陷的尝试:

  import tweepy, time

class TwitterAPI:
    def __init__(self):
        consumer_key = "aaaa"
        consumer_secret = "bbbb"
        auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
        access_token = "cccc"
        access_token_secret = "ddddd"
        auth.set_access_token(access_token, access_token_secret)
        self.api = tweepy.API(auth)

def tweet(self, message):
    self.api.update_status(status=message)
import csv
tweet_file_read = open('tweet6.csv')
csvreader = csv.DictReader(tweet_file_read)
tweet_file_check = open('tweetcheck.csv')
csvwriter= csv.DictWriter(tweet_file_check)
fieldnames = ['id', 'text', 'user']


if __name__ == "__main__":
    twitter = TwitterAPI()
    for tweet_file_read['id'] in csvreader:
        if tweet_file_read['id'] not in tweet_file_check['id']:
            twitter.api.retweet(tweet_file_read['id'])
            csv.writerow(tweet_file_read['id'])
            time.sleep(600)

我得到一个无效的语法:

csvwriter.writerow(tweet_file_read['id']) 

可能是因为我解决问题的方式存在系统性缺陷

非常感谢您的帮助!

【问题讨论】:

    标签: python csv twitter compare


    【解决方案1】:

    从我现在看到的情况来看,您的代码存在两个问题-

    1. csv.writerow(tweet_file_read['id']) 应该是

    csvwriter.writerow(tweet_file_read['id'])

    2. tweet_file_check = open('tweetcheck.csv') 应该是

    tweet_file_check = open('tweetcheck.csv', 'ab')

    (由于您试图在文件中追加一行,因此使用open 的默认模式以只读模式打开文件)

    【讨论】:

    • 谢谢,我做了调整,但它仍然给我同样的错误:S
    • @AmiYa 请使用当前代码和您遇到的错误的屏幕截图更新问题。
    猜你喜欢
    • 2020-07-21
    • 2016-06-29
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    相关资源
    最近更新 更多