我正在读一个csv,如下所示
data = pd.read_csv('news.csv')
它包含news
和category
列。我需要标记新闻专栏中的单词。
问题在于,新闻专栏的每一行文本的开头都包含b
b'Longevity Increase Seen Around the World: WHO'
b'Chikungunya spreading, mosquito-borne virus ...
我尝试了How do I get rid of the b-prefix in a string in python?,但这是字节编码字符串。所以
line = data['news'][0]
line.decode('utf-8')
将导致:
AttributeError: 'str' object has no attribute 'decode'
这些行中的每一行都属于str
类型。我如何移除那些b
此
b''
可能指向可以解码为字符串''
的字节类型,但也可能是内容为b'...'
的字符串本身对于第一种情况,您需要
line.decode()
,第二种情况需要line[2:-1]
相关问题 更多 >
编程相关推荐