2020-05-09
阅读量:
570
python 报错SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: tr

在windows系统当中读取文件路径可以使用\,但是在python字符串中\有转义的含义,如\t可代表TAB,\n代表换行,所以我们需要采取一些方式使得\不被解读为转义字符
可以按如下三种方式更改:
1、在路径前面加r,即保持字符原始值的意思
f=open(r"c:\Users\dongs\Desktop\python\drink_wine.txt",'r',enconding='utf-8')
2、使用两个反斜杠
f=open("c:\\Users\\dongs\\Desktop\\python\\drink_wine.txt",'r',enconding='utf-8')
3、替换为正斜杠
f=open("c:/Users/dongs/Desktop/python/drink_wine.txt",'r',enconding='utf-8')






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
3条评论