简体   繁体   中英

Python - Remove Square symbol from text string

a='ÿþ"[]B[]a[]l[]a[]n[]c[]e'

NOTE: The open and close square brackets represent this square symbol. I cannot however copy and paste the square symbol into here to show you exactly what I'm looking at.

The characters in 'a' represent the beginning of a file I've downloaded. It is a csv file, unicode. How do I remove these unwanted characters? I would just like to recover the word 'balance' from a.

The code I've used to simply this example:

fi = open(path+fn, 'r')
data = fi.read()
fi.close()
print(data)

Where fn is a csv file.

Tried:

data=data.encode()
d=replace('\x00','')

which produced error:

TypeError: expected bytes, bytearray or buffer compatible object

You need to specify the right encoding when opening the file. Try

open(path+fn, 'r', encoding="utf-16")

(I'm guessing utf-16 because ASCII characters seem to be encoded in two bytes in the sample string)

If you don't want to mess with encoding, string.printable is a list of 'printable' chars which may be what you're looking for.

>>> from string import printable
>>> best_string_ever = filter(lambda x: x in printable, a)
>>> best_string_ever
'"Balance'

If you can show the character value, then you can use the strip(u'\\uxxx\u0026#39;) command

use the replace() method

newstring = textstring.replace(u'\uxxx', '')

In this case pass in the actual character encoding that you want.

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