python print 打印unicode string的时候 去掉u' remove u'

时间:2022-02-24 20:11:35
1. 对list型的 用join 方法,是可以的实现的 myUlist = [u'AB'u'AB'u'AB'u'AB'] print myUlist

print ", ".join(myUlist)

[u'AB', u'AB', u'AB', u'AB']
AB, AB, AB, AB

2. 对json data 型的数据,用json.dump 方法, 是可以的实现的

read_json is {u'enableCwp': False, u'vendorId': u'', u'radioBand': u'DUAL', u'authenticationProtocol': u'PAP', u'userProfileAssignmentRules': []}

read_json = load_json_file(r'D:\psk-demo.json')
read_json_str = str(read_json).decode('utf-8')
print "read_json is %s end" % read_json_str
#print str({u"foo": "bar"})

turns = json.dumps(read_json)
print "turns is: %s " %turns

read_json is {u'enableCwp': False, u'vendorId': u'', u'radioBand': u'DUAL', u'authenticationProtocol': u'PAP', u'userProfileAssignmentRules': []}

turns is: {"enableCwp": false, "vendorId": "", "radioBand": "DUAL", "authenticationProtocol": "PAP", "userProfileAssignmentRules": []}


3. 对于单个string 串,可以用replace

def remove_uni(s):
"""remove the leading unicode designator from a string"""
s2 = ''
if s.startswith("u'"):
s2 = s.replace("u'", "'", 1)
elif s.startswith('u"'):
s2 = s.replace('u"', '"', 1)
return s2
s = 'u"you\'re my favorite string"'
sa = "u'mystring'"
print remove_uni(s)  # "you're my favorite string"
print remove_uni(sa) # 'mystring'

ref:

http://*.com/questions/761361/suppress-the-uprefix-indicating-unicode-in-python-strings http://*.com/questions/9773121/removing-u-in-list