Approximately converting unicode string to ascii string in python

后端 未结 5 1507
甜味超标
甜味超标 2020-12-24 14:04

don\'t know wether this is trivial or not, but I\'d need to convert an unicode string to ascii string, and I wouldn\'t like to have all those escape chars around. I mean, is

5条回答
  •  孤城傲影
    2020-12-24 14:43

    Try simple character replacement

    str1 = "“I am the greatest”, said Gavin O’Connor"
    print(str1)
    print(str1.replace("’", "'").replace("“","\"").replace("”","\""))
    

    PS: add # -*- coding: utf-8 -*- to the top of your .py file if you get error

提交回复
热议问题