Posts

Showing posts with the label Python reformat string data

Get the ASCII string for Decimal number in your data - reformat with Python

Once while writing a spider for a site, I came across some data that were not ASCII, rather had the ASCII value in the format '&#123' ... so I wrote a function myself to reformat the data (replace those with their corresponding ASCII values). But I had a feel that it was not Pythonic. So I searched Google, and found the following function that does the same task in a Pythonic way. Unfortunately I forgot from where I got it :( def asciirepl(self, match): "Return the ascii string for a decimal number" s = match.group() value = int(s[2:-1]) if value > 255: return '' return chr(value) def reformat_content(self, data): p = re.compile(r'&#(\d+);') return p.sub(self.asciirepl, data)