Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python dict包含字段名中不包含的字段_Python_Django_Csv_Serialization - Fatal编程技术网

Python dict包含字段名中不包含的字段

Python dict包含字段名中不包含的字段,python,django,csv,serialization,Python,Django,Csv,Serialization,这是我制作查询集的csv文件的函数 def esport_to_csv(self, tweets): with open('tweets.csv', 'w') as new_file: fieldnames = ["tweet_id", "text" , "user_screen_name", "user_name", "user_verified", "created_at", "user_time_zone", "user_location", "

这是我制作查询集的csv文件的函数

  def esport_to_csv(self, tweets):
        with open('tweets.csv', 'w') as new_file:
            fieldnames = ["tweet_id", "text" , "user_screen_name", "user_name", "user_verified", "created_at", "user_time_zone", "user_location", "favorite_count", "retweet_count", "user_followers_count", "user_friends_count"]
            csv_writer = csv.DictWriter(new_file, fieldnames=fieldnames, delimiter='\t')
            csv_writer.writeheader()
            for tweet in tweets:
                line = []
                line.append(tweet["tweet_id"])
                line.append(tweet["text"])
                line.append(tweet["user_screen_name"])
                line.append(tweet["user_name"])
                line.append(tweet["user_verified"])
                line.append(tweet["created_at"])
                line.append(tweet["user_time_zone"])
                line.append(tweet["user_location"])
                line.append(tweet["favorite_count"])
                line.append(tweet["retweet_count"])
                line.append(tweet["user_followers_count"])
                line.append(tweet["user_friends_count"])
                csv_writer.writerow(line)
这是我的服务器响应

ValueError: dict contains fields not in fieldnames: 967563194582515712, 'RT @KEEMSTAR: When you have your fathers car & you tell everyone on the internet that your 15 year old ass bought it. t.co/bUhhrPw0…', 'TKBrotherTK', 'Team KalvinYou need to pass a 
dict
to
DictWriter
, and tell it to ignore extra fields if they are present. Try this:

  def esport_to_csv(self, tweets):
    with open('tweets.csv', 'w') as new_file:
        fieldnames = ["tweet_id", "text" , "user_screen_name", "user_name", "user_verified", "created_at", "user_time_zone", "user_location", "favorite_count", "retweet_count", "user_followers_count", "user_friends_count"]
        csv_writer = csv.DictWriter(new_file, fieldnames=fieldnames, delimiter='\t', extrasaction='ignore')
        csv_writer.writeheader()
        for tweet in tweets:
            csv_writer.writerow(tweet)

ValueError:dict包含字段名中没有的字段:967563194582515712,'RT@KEEMSTAR:When you have your fathers car&;你在网上告诉所有人你15岁的屁股买了它。t、 co/bUhhrPw0…,“TKBrotherTK”,“Team Kalvin您需要将一个
dict
传递给
DictWriter
,并告诉它忽略存在的额外字段。试试这个:


... 为什么要将
列表
DictWriter
一起使用?因为tweet是字典类型
DictWriter
接受字典类型。。。这就是为什么它被称为'DictWriter'@avigil,但如果我这样做csv_writer.writerow(tweet)删除行(列表),我会得到同样的错误。我想问题在于字段名与数据匹配这是错误消息ValueError:dict包含字段名中不包含的字段:“截断”、“lang”、“user\u id”,“user\u utc\u offset”如果你的字典有额外的字段,你应该在创建
DictWriter
时传递
extrasaction='ignore'
,告诉它不要抱怨这些字段。你还可以告诉我如何附加到现有的csv文件中,此代码会覆盖附加模式下打开的现有字段:
open('tweets.csv',a')