TypeError: a bytes-like object is required, not 'str' , What should I do to fix it?

TypeError: a bytes-like object is required, not 'str' , What should I do to fix it?

我使用以下代码片段编写了 CSV。它有什么问题?

with open('%s_tweets.csv' % screen_name, 'wb') as f:
  writer = csv.writer(f)
  writer.writerow(["id","created_at","text","retweet_count","favorite_count"])
  writer.writerows(outtweets)

根据错误消息判断,您使用的是 Python 3 而不是 Python 2。当您使用 'b' 标志以二进制模式打开文件时,您说你将向它写入字节而不是 而不是 字符串。将模式标志中的 b 保留为 open,你应该没问题。