본문 바로가기

English Articles

Python solve partial of, SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position No1-No2: truncated \uXXXX escape

I wanted to fix the problem that Python path is not recognized well.

What I really wanted is to get any path(type str), fix it to a good path(type str).

However, it is nearly impossible to make my solution.

Nevertheles, I partially fixed some unicode escape problems using backslash+one-EN-alphabet.

Actually, the top3 best solutions are to use rawstring-form like r'abs_path', change one-backslash to either (front)slash or  double-backslashes.

 

It looks like just one-backslash in an invalid-argument, but it is actually an unicode-escape

 

The followings are code of replacing some unicode-escape(s). You can freely use it.

I saw someone else made a better code doing same, but I lost the actual online-address.

 

def get_partially_fixed_path(abs_path, converter='/'):
    ## assumption:
        # abs_path MUST NOT occur SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position No1-No2: truncated \uXXXX escape
        # I CANNOT solve 'unicodeescape' codec decode error, nor many of other users.
        # If you get 'unicodeescape' codec decode error, just use r'abs_path'. DO NOT TRY to solve this shit. It is waste of time
    ## function:
        # This function will replace the following 9 backslash-combined-letter
        # Other backslash-combined-letter(s) mostly not make a problem on reading path
        
        # can_manage_these= ['\a', '\b', '\f', '\n', '\o', '\p', '\r', '\s', '\t', '\v']
        # cannot magage_these_at_20221123= ['\u', '\x', '\N', '\U']

    ## inputs
        # abs_path: SKIP
        # Converter: The followings all work well.
            # basically one-slash, because it works well in both Linux and Windows
            # '\\'(==one-backslash). Note that, write backslash twice to get one-backslash.
            # '\\\\' or '\\'+'\\'(two-backslash)
            # multiple (front or back)slashes
    ## CODE
    replacement_from = ['\x07', '\x08', '\x0c', '\n', '\o', '\r', '\s', '\t', '\x0b']
    replacement_to = ['★a', '★b', '★f', '★n', '★o', '★r', '★s', '★t', '★v']
    # NOTE: Use different char(s) if your path contains '★'. Use '■↔■', 'gauza', or others not in your path

    if len(replacement_from) == len(replacement_to): # made this in case of if you manage replacement_lists.
        len_li = len(replacement_from)
    else:
        len_li = 0
        print('two lists are not equal')

    for i in range(len_li): 
        abs_path = abs_path.replace(replacement_from[i],replacement_to[i])
    abs_path = abs_path.replace('\\', '★') # one-backslash should be replaced at last.
    abs_path = abs_path.replace('★', converter)

    return(abs_path)

## RUN
# just use funcion and do your job.
# sample code is in below.
original = "D:\python\aa\bb\ff\nn\rr\ss\tt\vv\gazua.txt"
path = get_partially_fixed_path(original, '\\\\')
with open(path, 'r', encoding='utf-8') as temp:
    res = temp.read()
print(res)

 

Use a list, containing from a to z, and A to Z
a2zA2z = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

I failed on following ways. Just look it to SAVE YOUR TIME.

these will never works

# I certain you cannot manage unicode escape if it is 

fail_str_0 = "D:\python\aa\bb\ff\nn\tt\rr\uu"
fail_str_1 = "D:\python\aa\bb\ff\nn\tt\rr\uu".encode('utf-8').strip()

fail_arr_1 = ["D:\python\aa\bb\ff\nn\tt\rr\uu"]

uni_esc_err= [r'\a', r'\b', r'\f', r'\n', r'\p', r'\r', r'\s', r'\t', r'\u', r'\v', r'\x', r'\N', r'\U']
for i in range(len(uni_esc_err)):
    try:
        stt = stt.replace(uni_esc_err[i], '/')
    except:
        print('fail')

def work_damn_1(any_str):
    try:
        res = str(any_str)
    return(res)
def work_damn_2(any_str):
    try:
        res = r'{}'.format(any_str)
    except:
        print('fail1')
        pass
    return(res)
def work_damn_3(any_str):
    try:
        res = repr(any_str)
    except:
        pass
    return(res)

with open('"D:\python\empty.txt', 'w') as f:
     f.write("D:\python\aa\bb\ff\nn\tt\rr\uu")
     f.close()