0

I am trying to print a string in Python 2.7 but the string contains the character "(150\xb5s)" and it cannot print x5bs and so it gives error for that position of the string.

UnicodeEncodeError: "ascii" codec can't encode character u'\xb5' 
string_print = "29 days in (F150\xb5s)"
with open("testing_file.txt", "w") as fill:
    fill.write(string_print)

I tried doing this from stackoverflow solution but still got same error

string_print = "29 days in (F150\xb5s)"

with open("testing_file.txt", "w") as fill:
    fill.write(string_print.decode("utf-8"))

using a raw string method works but the problem is that string_print can have any string as I am just making a very long program short to better isolate the problem. So is there a way to make the variable string_print a part of raw string. If I just do raw string of "29 days in (F150\xb5s)" it works.

r"29 days in (F150\xb5s)"

But string_print can have any type of string

Ashish
  • 75
  • 1
  • 9
  • 1
    Have you tried anything from here : https://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20 – Rishabh Kumar Feb 22 '21 at 18:11
  • 1
    Also consider using Python 3. See [https://www.python.org/doc/sunset-python-2/](https://www.python.org/doc/sunset-python-2/) – Hernán Alarcón Feb 22 '21 at 20:10
  • When I run that code in Python 2.7 it just works, and gives the result `robot-1955Trying to print the character(150µs)` – BoarGules Feb 22 '21 at 21:25
  • @BoarGules I have rephrased the question where I am trying to write the string into a file which is actually causing the problem. – Ashish Feb 23 '21 at 01:49
  • 1
    Does this answer your question? ["UnicodeEncodeError: 'ascii' codec can't encode character"](https://stackoverflow.com/questions/1652904/unicodeencodeerror-ascii-codec-cant-encode-character) – Håken Lid Feb 23 '21 at 02:01
  • @RishabhKumar I am trying to use that solution but I cannot seem to solve the problem and I get same error. Would you please help? error: UnicodeDecodeError: 'ascii' codec can't decode byte 0xb5 in position 0: ordinal not in range(128) – Ashish Feb 23 '21 at 02:05
  • @HåkenLid I tried it and still failed with the same error – Ashish Feb 23 '21 at 02:20
  • @BoarGules It is not supposed to show 150µs. It is supposed to show the same as shown in the string_print i.e same as "29 days in (F150\xb5s)" – Ashish Feb 23 '21 at 02:22
  • You must encode to utf8, not decode. `string_print.encode("utf-8")`. If you want to use python 2, it's important to understand text encodings. – Håken Lid Feb 23 '21 at 02:23
  • @HåkenLid Still the same error: UnicodeDecodeError: 'utf8' codec can't decode byte 0xb5 in position 16: invalid start byte. Note I want the same exact string to be written in .txt file i.e "29 days in (F150\xb5s)" – Ashish Feb 23 '21 at 02:30
  • Use a raw string `r"29 days in (F150\xb5s)"` and you can write using ascii. This prevents `\xb5` from being interpreted as a unicode escape sequence. https://docs.python.org/2.7/reference/lexical_analysis.html#string-literals – Håken Lid Feb 23 '21 at 02:32
  • @HåkenLid Hey that works but string_print can have any string inserted in that variable. So not necessarily "29 days in (F150\xb5s)". So how do I make sure that r i.e raw string is always in front of the string in variable string_print? – Ashish Feb 23 '21 at 02:39

1 Answers1

0

I have found a solution to this problem. Basically we can use repr(string_print) to solve this issue.

string_print = "29 days in (F150\xb5s)"
with open("testing_file.txt", "w") as fill:
    fill.write(repr(string_print))
Ashish
  • 75
  • 1
  • 9