a bytes-like object is required, not 'str'怎么解决?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了a bytes-like object is required, not 'str'怎么解决?相关的知识,希望对你有一定的参考价值。

import socket
import struct
import time
import win32api
TimeServer = '210.72.145.44' #国家授bai时中心ip
Port = 123
def getTime():
TIME_1970 = 2208988800
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
data = '\x1b' + 47 * '\0'
client.sendto(data, (TimeServer, Port))
data, address = client.recvfrom(1024)
data_result = struct.unpack('!12I', data)[10]
data_result -= TIME_1970
return data_result

这些代码中在 data = '\x1b' + 47 * '\0'这一行总是报a bytes-like object is required, not 'str'的问题,这个是怎么解决啊,谢谢

参考技术A

可以尝试用bytes将str类型的数据转成bytes类型的数据, 如上图所示

希望能够帮到你!!

如何解决此代码中的“TypeError: a bytes-like object is required, not 'str'”? [复制]

【中文标题】如何解决此代码中的“TypeError: a bytes-like object is required, not \'str\'”? [复制]【英文标题】:How to resolve "TypeError: a bytes-like object is required, not 'str' " in this code? [duplicate]如何解决此代码中的“TypeError: a bytes-like object is required, not 'str'”? [复制] 【发布时间】:2020-11-03 06:33:41 【问题描述】:

我对 Python 比较陌生。我有这段代码,它使用 Hashtags 从 Twitter 中提取数据并将它们保存在 CSV 文件中。它在 Python 2.7 中运行良好,但是当我尝试在 Python 3 中运行它时,它给了我TypeError: a bytes-like object is required, not 'str',如下所示:

import json
import csv
import tweepy
import re
"""
INPUTS:
    consumer_key, consumer_secret, access_token, access_token_secret: codes 
    telling twitter that we are authorized to access this data
    hashtag_phrase: the combination of hashtags to search for
OUTPUTS:
    none, simply save the tweet info to a spreadsheet
"""
def search_for_hashtags(consumer_key, consumer_secret, access_token, access_token_secret, hashtag_phrase):
    
    #create authentication for accessing Twitter
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)

    #initialize Tweepy API
    api = tweepy.API(auth)
    
    #get the name of the spreadsheet we will write to
    fname = '_'.join(re.findall(r"#(\w+)", hashtag_phrase))

    #open the spreadsheet we will write to
    with open('%s.csv' % (fname), 'wb') as file:

        w = csv.writer(file)

        #write header row to spreadsheet
        w.writerow(['timestamp', 'tweet_text', 'username', 'all_hashtags', 'followers_count'])

        #for each tweet matching our hashtags, write relevant info to the spreadsheet
        for tweet in tweepy.Cursor(api.search, q=hashtag_phrase+' -filter:retweets', \
                                   lang="en", tweet_mode='extended').items(100):
            w.writerow([tweet.created_at, tweet.full_text.replace('\n',' ').encode('utf-8'), tweet.user.screen_name.encode('utf-8'), [e['text'] for e in tweet._json['entities']['hashtags']], tweet.user.followers_count])
consumer_key = input('Consumer Key ')
consumer_secret = input('Consumer Secret ')
access_token = input('Access Token ')
access_token_secret = input('Access Token Secret ')
    
hashtag_phrase = input('Hashtag Phrase ')

if __name__ == '__main__':
    search_for_hashtags(consumer_key, consumer_secret, access_token, access_token_secret, hashtag_phrase)

【问题讨论】:

感谢您的回复。我认为确实如此。但不幸的是,我现在又遇到了另一个错误:ibb.co/qpBJkNs(单击链接查看错误)。请看看您是否能够理解问题所在。 看看here. 【参考方案1】:

尝试改变

with open('%s.csv' % (fname), 'wb') as file:

进入

with open('%s.csv' % (fname), 'w') as file:

【讨论】:

感谢您的回复。但是现在,我收到以下错误:ibb.co/qpBJkNs(点击链接查看错误)

以上是关于a bytes-like object is required, not 'str'怎么解决?的主要内容,如果未能解决你的问题,请参考以下文章

python问题:TypeError: a bytes-like object is required, not 'str'

a bytes-like object is required, not 'str'怎么解决?

python 3.5: TypeError: a bytes-like object is required, not 'str'

TypeError: a bytes-like object is required, not 'str'

Python 3.5.2 TypeError: a bytes-like object is required, not 'str’问题解决方案

a bytes-like object is required,not str'怎么解决?