总述:
介绍几种拼接中文路径和英文路径的常用方式,和几种坑。如果没时间看坑,直接跳到最后即可
常见错误:
1.通过"{}{}{}".format()来拼接
path1 = "D:/中文/"
path2 = "test/"
def test_road(path1,path2):
path = "{}{}{}".format(path1,path2,"hhh.txt")
with open(path,'rb')as f:
print f.read()
确实可以拼接成功,不过路径变成了酱样子
IOError: [Errno 2] No such file or directory: 'D:/\xe4\xb8\xad\xe6\x96\x87/test/hhh.txt'
怎么办呢?一般中文可以通过unicode来强制编码解决
path1 = "D:/中文/"
path2 = "test/"
def test_road(path1,path2):
path1 = unicode(path1,"utf8") #加入这一句,强制把格式转化为utf8
path = "{}{}{}".format(path1,path2,"hhh.txt")
with open(path,'rb')as f:
print f.read()
出现了以下错误:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 3-4: ordinal not in range(128)
这下没辙了,换过一种字符串的拼接方式吧。字符串可以直接用+号拼接
path1 = "D:/中文/"
path2 = "test/"
def test_road(path1,path2):
path1 = unicode(path1,"utf8")
# path = "{}{}{}".format(path1,path2,"hhh.txt")
path = path1+path2+"hhh.txt" #换过一种字符串拼接方式
with open(path,'rb')as f:
print f.read()
这次就成功了。
总结:
python里面中英文字符串拼接的时候要注意:
1.使用+号拼接字符串,
2.在连接之前把含有中文的字符串强制转码,path = unicode(path,'utf8')
成功案例:
path1 = "D:/中文/"
path2 = "test/"
def test_road(path1,path2):
path1 = unicode(path1,"utf8")
path = path1+path2+"hhh.txt"
with open(path,'rb')as f:
print f.read()
在拼接之后才转码也是可以的
path1 = "D:/中文/"
path2 = "test/"
def test_road(path1,path2):
path = path1+path2+"hhh.txt"
path = unicode(path,'utf8') #在完成拼接之后转码
with open(path,'rb')as f:
print f.read()