马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代碼#! /usr/bin/env python3
f = open('OpenMe.mp3', 'rb')
for each_line in f:
print(each_line, end='')
f.close()
打印b'\xb3\xa4\xc6\xda\xca\xb9\xd3\xc3Windows\xb2\xd9\xd7\xf7\xcf\xb5\xcd\xb3\xb5\xc4\xc5\xf3\xd3\xd1\xba\xdc\xc8\xdd\xd2\xd7\xb1\xbb\xc0\xa9\xd5\xb9\xc3\xfb\xcb\xf9\xce\xf3\xb5\xbc\xa3\xac\xc8\xcf\xce\xaa\xc0\xa9\xd5\xb9\xc3\xfb\xbe\xf6\xb6\xa8\xce\xc4\xbc\xfe\xc0\xe0\xd0\xcd\xa3\xac\xc6\xe4\xca\xb5\xd5\xe2\xd6\xd6\xb9\xdb\xc4\xee\xca\xc7\xb4\xed\xce\xf3\xb5\xc4\xa3\xa1\r\n'b'\r\n'b'\xc6\xe4\xca\xb5\xd5\xe2\xbe\xcd\xb8\xfa\xd2\xbb\xb8\xf6\xd0\xd5\xd5\xc5\xb5\xc4\xbb\xb5\xc8\xcb\xa3\xac\xbe\xa1\xb9\xdc\xb0\xd1\xc3\xfb\xd7\xd6\xb8\xc4\xce\xaa\xc1\xcb\xa1\xb0\xd5\xc5\xba\xc3\xc8\xcb\xa1\xb1\xa3\xac\xb5\xab\xcb\xfb\xbb\xb9\xca\xc7\xd2\xbb\xb8\xf6\xbb\xb5\xc8\xcb\xca\xc7\xd2\xbb\xb8\xf6\xb5\xc0\xc0\xed\xb5\xc4^_^\r\n'b'\r\n'b'\xb9\xd8\xd3\xda\xce\xc4\xbc\xfe\xb5\xc4\xc0\xa9\xd5\xb9\xc3\xfb\xa3\xac\xb3\xf5\xd1\xa7\xd5\xdf\xc8\xdd\xd2\xd7\xd7\xdf\xbd\xf8\xb5\xc4\xce\xf3\xc7\xf8\xa3\xba\r\n'b'\r\n'b'\xce\xf3\xc7\xf8\xd2\xbb\xa3\xba\xce\xc4\xbc\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bf\xb7\xd6\xa3\xac\xb5\xab\xd2\xbb\xb8\xf6\xce\xc4\xbc\xfe\xb2\xa2\xb2\xbb\xd2\xbb\xb6\xa8\xd0\xe8\xd2\xaa\xd2\xbb\xb8\xf6\xc0\xa9\xd5\xb9\xc3\xfb\xa1\xa3\r\n'b'\r\n'b'\xce\xf3\xc7\xf8\xb6\xfe\xa3\xba\xce\xc4\xbc\xfe\xc0\xa9\xd5\xb9\xc3\xfb\xb1\xed\xc3\xf7\xc1\xcb\xb8\xc3\xce\xc4\xbc\xfe\xca\xc7\xba\xce\xd6\xd6\xc0\xe0\xd0\xcd\r\n'b'\r\n'b'\xce\xc4\xbc\xfe\xc0\xa9\xd5\xb9\xc3\xfb\xbf\xc9\xd2\xd4\xc8\xcb\xce\xaa\xc9\xe8\xb6\xa8\xa3\xac\xc0\xa9\xd5\xb9\xc3\xfb\xce\xaaTXT\xb5\xc4\xce\xc4\xbc\xfe\xd3\xd0\xbf\xc9\xc4\xdc\xca\xc7\xd2\xbb\xd5\xc5\xcd\xbc\xc6\xac\xa3\xac\xcd\xac\xd1\xf9\xa3\xac\xc0\xa9\xd5\xb9\xc3\xfb\xce\xaaM-P3\xb5\xc4\xce\xc4\xbc\xfe\xa3\xac\xd2\xc0\xc8\xbb\xbf\xc9\xc4\xdc\xca\xc7\xd2\xbb\xb8\xf6\xca\xd3\xc6\xb5\xa1\xa3'
代碼
(由[课后作业] 第028讲:文件:因为懂你,所以永恒 | 课后测试题及答案的解答直接複製)f = open('OpenMe.mp3')
for each_line in f:
print(each_line, end='')
f.close()
報錯Traceback (most recent call last):
File "D:\program-training\python-training\use2.py", line 4, in <module>
for each_line in f:
UnicodeDecodeError: 'cp950' codec can't decode byte 0xc8 in position 31: illegal multibyte sequence
用utf-8 encodingf = open('OpenMe.mp3', encoding='utf-8')
for each_line in f:
print(each_line, end='')
f.close()
報錯Traceback (most recent call last):
File "D:\program-training\python-training\use2.py", line 4, in <module>
for each_line in f:
File "D:\software\python\lib\codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 0: invalid start byte
到底是要打印出來啥,是上面那一大串二進制的東西嗎?
有辦法解決報錯問題嗎?
以前有發過一次文,和一位魚友講著講著就沒下文了
當時就那樣放著了(這個mp3也因為這樣在我的桌面放了快2年哈哈)
希望這次能有魚友能給一些新思路
(我也看了一些英文網站,像是stack overflow,
但大家情況都不太一樣,版本環境啥的也有影響,
看了一圈還是沒有解決我的疑惑...)
#! /usr/bin/env python3
f = open('OpenMe.mp3', 'rb')
for each_line in f:
print(each_line, end='')
f.close()
b'\xb3\xa4\xc6\xda\xca\xb9\xd3\xc3Windows\xb2\xd9\xd7\xf7\xcf\xb5\xcd\xb3\xb5\xc4\xc5\xf3\xd3\xd1\xba\xdc\xc8\xdd\xd2\xd7\xb1\xbb\xc0\xa9\xd5\xb9\xc3\xfb\xcb\xf9\xce\xf3\xb5\xbc\xa3\xac\xc8\xcf\xce\xaa\xc0\xa9\xd5\xb9\xc3\xfb\xbe\xf6\xb6\xa8\xce\xc4\xbc\xfe\xc0\xe0\xd0\xcd\xa3\xac\xc6\xe4\xca\xb5\xd5\xe2\xd6\xd6\xb9\xdb\xc4\xee\xca\xc7\xb4\xed\xce\xf3\xb5\xc4\xa3\xa1\r\n'b'\r\n'b'\xc6\xe4\xca\xb5\xd5\xe2\xbe\xcd\xb8\xfa\xd2\xbb\xb8\xf6\xd0\xd5\xd5\xc5\xb5\xc4\xbb\xb5\xc8\xcb\xa3\xac\xbe\xa1\xb9\xdc\xb0\xd1\xc3\xfb\xd7\xd6\xb8\xc4\xce\xaa\xc1\xcb\xa1\xb0\xd5\xc5\xba\xc3\xc8\xcb\xa1\xb1\xa3\xac\xb5\xab\xcb\xfb\xbb\xb9\xca\xc7\xd2\xbb\xb8\xf6\xbb\xb5\xc8\xcb\xca\xc7\xd2\xbb\xb8\xf6\xb5\xc0\xc0\xed\xb5\xc4^_^\r\n'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4\xad\xd2\xf2\xb2\xce\xbf\xb4\xcf\xc2\xc3\xe6\xb5\xc4\xb5\xda2\xb8\xf6\xce\xf3\xc7\xf8\xa3\xa9\xa3\xac\xce\xc4\xbc\xfe\xc0\xa9\xd5\xb9\xc3\xfb\xca\xc7\xd2\xbb\xb8\xf6\xb3\xa3\xb9\xe6\xce\xc4\xbc\xfe\xb5\xc4\xb9\xb9\xb3\xc9\xb2\xbf\xb7\xd6\xa3\xac\xb5\xab\xd2\xbb\xb8\xf6\xce\xc4\xbc\xfe\xb2\xa2\xb2\xbb\xd2\xbb\xb6\xa8\xd0\xe8\xd2\xaa\xd2\xbb\xb8\xf6\xc0\xa9\xd5\xb9\xc3\xfb\xa1\xa3\r\n'b'\r\n'b'\xce\xf3\xc7\xf8\xb6\xfe\xa3\xba\xce\xc4\xbc\xfe\xc0\xa9\xd5\xb9\xc3\xfb\xb1\xed\xc3\xf7\xc1\xcb\xb8\xc3\xce\xc4\xbc\xfe\xca\xc7\xba\xce\xd6\xd6\xc0\xe0\xd0\xcd\r\n'b'\r\n'b'\xce\xc4\xbc\xfe\xc0\xa9\xd5\xb9\xc3\xfb\xbf\xc9\xd2\xd4\xc8\xcb\xce\xaa\xc9\xe8\xb6\xa8\xa3\xac\xc0\xa9\xd5\xb9\xc3\xfb\xce\xaaTXT\xb5\xc4\xce\xc4\xbc\xfe\xd3\xd0\xbf\xc9\xc4\xdc\xca\xc7\xd2\xbb\xd5\xc5\xcd\xbc\xc6\xac\xa3\xac\xcd\xac\xd1\xf9\xa3\xac\xc0\xa9\xd5\xb9\xc3\xfb\xce\xaaM-P3\xb5\xc4\xce\xc4\xbc\xfe\xa3\xac\xd2\xc0\xc8\xbb\xbf\xc9\xc4\xdc\xca\xc7\xd2\xbb\xb8\xf6\xca\xd3\xc6\xb5\xa1\xa3'
这个是因为你使用 'rb' 打开的文件
就是你用二进制打开的,可以看到输出的字符串最前面有一个字母 b
说明这个字符串是二进制形式的
f = open('OpenMe.mp3')
for each_line in f:
print(each_line, end='')
f.close()
$ cat main.py
f = open('OpenMe.mp3')
for each_line in f:
print(each_line, end='')
f.close()
$ python main.py
Traceback (most recent call last):
File "/tmp/main.py", line 2, in <module>
for each_line in f:
File "/usr/lib/python3.10/codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 0: invalid start byte
$
这个我这边也报错
如果你在open的参数中不指定编码,默认就使用当前系统的编码,我这个系统使用的utf-8编码
而OpenMe.mp3这个文件的编码是gb18030,怎么知道的?我用vim打开这个文件,右下角显示这个文件的编码是gb18030
$ cat main.py
f = open('OpenMe.mp3', encoding='gb18030')
for each_line in f:
print(each_line, end='')
f.close()
$ python main.py
长期使用Windows操作系统的朋友很容易被扩展名所误导,认为扩展名决定文件类型,其实这种观念是错误的!
其实这就跟一个姓张的坏人,尽管把名字改为了“张好人”,但他还是一个坏人是一个道理的^_^
关于文件的扩展名,初学者容易走进的误区:
误区一:文件扩展名是一个文件的必要构成部分
一个文件可以有或没有扩展名,对于打开文件操作,没有扩展名的文件需要选择程序去打开它,有扩展名的文件会自动用设置好的程序(如有)去尝试打开(是“尝试打开”,而不是“打开”的原因参看下面的第2个误区),文件扩展名是一个常规文件的构成部分,但一个文件并不一定需要一个扩展名。
误区二:文件扩展名表明了该文件是何种类型
文件扩展名可以人为设定,扩展名为TXT的文件有可能是一张图片,同样,扩展名为M-P3的文件,依然可能是一个视频。$
|