# Python code automatically converts characters to ASCIi

**URL:** <https://community.openai.com/t/python-code-automatically-converts-characters-to-ascii/1261505>\
**Category:** Prompting\
**Tags:** gpt-4\
**Created:** [May 15, 2025, 6:39am UTC](https://community.openai.com/t/python-code-automatically-converts-characters-to-ascii/1261505 "2025-05-15T06:39:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Allen\_Campbell](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/allen_campbell/32/630137_2.png) [@Allen\_Campbell](https://community.openai.com/u/Allen_Campbell)\
**Post date:** [May 15, 2025, 6:39am UTC](https://community.openai.com/t/python-code-automatically-converts-characters-to-ascii/1261505/1 "2025-05-15T06:39:29Z")

</div>

![A smartphone screenshot shows a "Select Text" screen displaying a Python code snippet for reading and cleaning a file using regular expressions. (Captioned by AI)](https://us1.discourse-cdn.com/openai1/original/4X/7/3/e/73e3be7ffddf10f7d7091fa697283c3c9ba16d43.png)

cleaned\_content = re.sub(r’\x1b

```
 notice the ASCII characters after =re sub(r\x1b  

```

??? The code is supposed to be standard UTF-8  
BUT  
somewhere between Chat-GPTs neural network &“mind && fingertips” the code / response is being converted to ASCII characters which pydroid3, Termux & other tools don’t interpret  
sooo when Chat-GPT sends me the corrected version it still is in ASCII

I’ve tried running it on play store app, in chrome, Firefox etc.

this is problematic probably for a lot of people. ▪  
Even when I output clean **UTF-8-compliant Python** , some characters — especially **escaped sequences like `\x1b`** , `\u`, `\r`, `\n`, or even backslashes — get silently corrupted _in transit_:

- **Android input methods** (IME) may normalize or escape characters
- **Chrome/Firebase/Clipboard APIs** sometimes auto-convert or truncate escape sequences
- **TextView components** inside apps like Pydroid3 re-interpret `\x` as literal control characters instead of raw text
- **Some fonts** in Android may misrender invisible control glyphs

This is why what looks like:

```python
cleaned_content = re.sub(r'\x1b
```

---

<div class="post-metadata">

**Author:** ![\_j](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/_j/32/766292_2.png) [@\_j](https://community.openai.com/u/_j)\
**Post date:** [May 15, 2025, 7:57am UTC](https://community.openai.com/t/python-code-automatically-converts-characters-to-ascii/1261505/2 "2025-05-15T07:57:32Z")

</div>

Why u do that?

```python
import re

input_path = '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/.bashrc.py'
output_path = '/storage/emulated/0/Android/data/ru.iiec.pydroid3/files/.bashrc_cleaned.py'

with open(input_path, 'r', encoding='utf-8', errors='ignore') as f:
    content = f.read()

cleaned_content = re.sub(r'\x1b

```

That is an escape character. `chr(27)`

Being in the first 127 code points of UTF-8, ASCII and UTF-8 are the same single bytes.

I don’t know where the incomplete line you show came from. However, I can answer the “why” it could be there or could be predicted.

The ESC (`\x1b`) character is used to introduce **ANSI escape sequences** for text formatting and terminal control, which are also both ASCII and UTF-8.

`print("\x1b[1mThis is bold text.\x1b[0m")`

I have a streaming markdown to color reformatter that even employs them.

* * *

The AI shouldn’t generally be making them. It writes in readable text, markdown. You could strip all bytes 0-31 (apart from tabs or linefeeds), but then were it actually ANSI control that continued, or even printer control, you’d be left with the remainder.

Can you explain more clearly what you think the problem is and where it is coming from, and the resulting symptom?
