Splicing of Chinese path and English path in python

General statement:

This paper introduces several common ways of splicing Chinese and English paths, and several pits. If you don't have time to see the pit, just jump to the end

Common errors:

1. Splice by "{} {}" {} ". format()

path1 = "D:/Chinese/"
path2 = "test/"
def test_road(path1,path2):
    path = "{}{}{}".format(path1,path2,"hhh.txt")
    with open(path,'rb')as f:
        print f.read()

It can be spliced successfully, but the path becomes sauce

IOError: [Errno 2] No such file or directory: 'D:/\xe4\xb8\xad\xe6\x96\x87/test/hhh.txt'

What shall I do? Common Chinese can be forced to be encoded by unicode

path1 = "D:/Chinese/"
path2 = "test/"
def test_road(path1,path2):
    path1 = unicode(path1,"utf8")   #Add this sentence to force the format to utf8
    path = "{}{}{}".format(path1,path2,"hhh.txt")
    with open(path,'rb')as f:
        print f.read()

The following error occurred:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 3-4: ordinal not in range(128)

There's no way. Let's use a string splicing method. String can be spliced directly with + sign

path1 = "D:/Chinese/"
path2 = "test/"
def test_road(path1,path2):
    path1 = unicode(path1,"utf8")
    # path = "{}{}{}".format(path1,path2,"hhh.txt")
    path = path1+path2+"hhh.txt"   #Another string splicing method
    with open(path,'rb')as f:
        print f.read()

It was a success this time.

Conclusion:

Note when splicing Chinese and English strings in python:

1. Use the + sign to splice the string,

2. Before connecting, the string containing Chinese is forced to transcode, path = unicode(path,'utf8')

Success stories:

path1 = "D:/Chinese/"
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()

It's OK to transcode after splicing

path1 = "D:/Chinese/"
path2 = "test/"
def test_road(path1,path2):
    path = path1+path2+"hhh.txt"
    path = unicode(path,'utf8')   #Transcode after splicing
    with open(path,'rb')as f:
        print f.read()

 

Keywords: ascii codec Python

Added by aspguy on Tue, 31 Dec 2019 22:01:52 +0200