大雨带走从前 发表于 2020-9-24 01:21:36

关于Python 3.8.5如何使用read()进行文件的读取!!!

不知道是版本问题还是我操作问题,我无法使用对文件进行操作的那些方法,如:读取read,定位tell等等 ,望大神解惑!!!   注:(实验1.TXT的内容为: 偶尔还是会心痛)






Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) on win32
Type "help", "copyright", "credits" or "license()" for more information.

>>> f = open(r"C:\Users\32386\Desktop\实验1.txt")
>>> f.read()
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
    f.read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0x9b in position 20: incomplete multibyte sequence
>>> f = open("C:\Users\32386\Desktop\实验1.txt",'r')
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
>>> f = open(r"C:\Users\32386\Desktop\实验1.txt")
>>> f,read()
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
    f,read()
NameError: name 'read' is not defined
>>> f.tell()
0
>>> f.read(3)
'鍋跺皵'
>>> f.read(1)
'杩'
>>> f.read(2)
'樻槸'
>>> f.read(6)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
    f.read(6)
UnicodeDecodeError: 'gbk' codec can't decode byte 0x9b in position 0: incomplete multibyte sequence
>>> list(f)
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
    list(f)
UnicodeDecodeError: 'gbk' codec can't decode byte 0x9b in position 0: incomplete multibyte sequence
>>>

heidern0612 发表于 2020-9-24 07:27:35

f = open(r"C:\Users\32386\Desktop\实验1.txt", encoding="utf-8")

试试
页: [1]
查看完整版本: 关于Python 3.8.5如何使用read()进行文件的读取!!!