简体   繁体   中英

Is there a way to ignore escape sequences in python?

import re
import math
def encode(n, strng):

    for z in range (n):
        temp = []
        spcPos = []


        for i , x in enumerate(strng):
            if x == ' ':
                spcPos.append(i)
        strng = re.sub(' ', '', strng)

        for i in range (len(strng)):
            temp.append(strng[i - n])

        temp2 = []

        ezEnu = 0

        for i in temp:
            if ezEnu in (spcPos):
                temp2.append(' ')
                ezEnu += 1
            temp2.append(i)
            ezEnu += 1

        temp2 = ''.join(temp2)
        temp2 = temp2.split()
        temp = []

        withSpc = []

        withSpc2 = []

        oglen = []

        for i in temp2:
            oglen.append(len(i))
            for x in range (math.ceil(n / len(i))):
                withSpc.append(i)
            withSpc2.append(''.join(withSpc))
            withSpc = []

        newa = []

        for i, x in enumerate(withSpc2):
            for y in range(int(oglen[i])):
                newa.append(x[y - n])

        temp2 = []

        ezEnu = 0

        for i in newa:
            if ezEnu in (spcPos):
                temp2.append(' ')
                ezEnu += 1
            temp2.append(i)
            ezEnu += 1

        strng = ''.join(temp2)

    return(''.join([str(n), ' ', strng]))

How can I take a string, and make it so the escape sequences are treated as regular characters?

I have this function encode that takes a string and encodes it. The problem is when I receive strings that have a / in or any escape sequence, it ignores them and does not treat it as part of the message to encode.

I have no control over the strings that come into the function, they are predefined.

From what I understand r'' only works on new strings.

Thanks:)

depending on what you want exactly you might check whether this is sufficient for your use case.

def encode(n, strng):
    return repr(strng)[1:-1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM