angry 发表于 2020-12-11 20:08:45

文件打开

f = open('C:\Users\Administrator\Desktop\OpenMe.mp3')
for each_line in f:
         print(each_line, end ='')

f.close()
我文件放在上面所示的路径下,但是报错了,(unicode error)'unicodeescape' codec can't decode bytes in position 2-3:truncated\UXXXXXXXX escape

逃兵 发表于 2020-12-11 20:26:11

路径的问题
你改一下

f = open(r'C:\Users\Administrator\Desktop\OpenMe.mp3')
for each_line in f:
         print(each_line, end ='')

f.close()

疾风怪盗 发表于 2020-12-11 22:24:55

百度搜一下,就有答案了:

在读取桌面文件的时候一直报错:(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape


自己刚开始写的是这样的:

import scipy as sp
data = sp.genfromtxt("C:\Users\Administrator\Desktop\Machine Learning\web_traffic.tsv",delimiter="\t")


后来发现,在Python中\是转义符,\u表示其后是UNICODE编码,因此\User在这里会报错,在字符串前面加个r表示就可以了

import scipy as sp
data = sp.genfromtxt(r"C:\Users\Administrator\Desktop\Machine Learning\web_traffic.tsv",delimiter="\t")

angry 发表于 2020-12-12 14:40:07

逃兵 发表于 2020-12-11 20:26
路径的问题
你改一下

谢谢大佬
页: [1]
查看完整版本: 文件打开