简体   繁体   中英

Remove Special Character from tuple of list

I want to remove special characters from tuple. Consider an example

(['\x0cSome Namel\n'],['\x0c4739 2332 3450 1111\n'])

I want to get output to be

([Some Name ],[4739 2332 2450 1111])

I tried using split and replace even after using that it is returning same output

Consider you have following string on input:

string = r"""['\x0cSome Namel\n'] ['\x0c4739 2332 3450 1111\n']"""

In this case you can use replace function:

string = string.replace(r"'\x0c", "").replace(r"\n'", "")

Output:

[Some Namel] [4739 2332 3450 1111]

If you specifically want to remove the two characters shown in the question and if their position in each string is irrelevant then:

mytuple = ['\x0cSome Namel\n'], ['\x0c4739 2332 3450 1111\n']

for te in mytuple:
    for i, s in enumerate(te):
        te[i] = s.replace('\n', '').replace('\f', '')

print(mytuple)

Output:

(['Some Namel'], ['4739 2332 3450 1111'])

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