从python3中的文件读取字节字符串 [英] read bytes string from file in python3

查看:60
本文介绍了从python3中的文件读取字节字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件内容如下,文件编码为utf-8:

The content of a file is like following, and the file encoding is utf-8:

cd232704-a46f-3d9d-97f6-67edb897d65f    b'this Friday, Gerda Scheuers will be excited \xe2\x80\x94 but she\xe2\x80\x99s most excited about the merchandise the movie will bring.'

这是我的代码:

with open(file, 'r') as f_in:
    for line in f_in:
        tokens = line.split('\t')
        print(tokens[1])

我想得到正确的答案-这个星期五,格尔达·舒伊斯(Gerda Scheuers)会很兴奋-但她对这部电影将带来的商品感到最兴奋."

I want to get the right answer - "this Friday, Gerda Scheuers will be excited - but she's most excited about the merchandise the movie will bring."

print(b'\xe2\x80\x94'.decode('utf-8')) #convert into ASCII 

但是我无法从文件中读取字节.如果我打开一个包含字节的文件,则需要对行进行解码以将其分割.

But I can't read the bytes from a file. If I open a file with bytes, I need to decode the line to splite it.

推荐答案

您可以使用

You can use ast.literal_eval to convert the bytes literal to bytes:

然后,对其进行解码以获取字符串对象:

Then, decode it to get string object:

>>> ast.literal_eval(r"b'excited \xe2\x80\x94 but she\xe2\x80\x99s'")
b'excited \xe2\x80\x94 but she\xe2\x80\x99s'
>>> ast.literal_eval(r"b'excited \xe2\x80\x94 but she\xe2\x80\x99s'").decode('utf-8')
'excited — but she’s'


with open(file, 'r') as f_in:
    for line in f_in:
        tokens = line.split('\t')
        # if len(tokens) < 2:
        #    continue
        bytes_part = ast.literal_eval(tokens[1])
        s = bytes_part.decode('utf-8')  # Decode the bytes to convert to a string

这篇关于从python3中的文件读取字节字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆