1. 程式人生 > >python print 列印unicode string的時候 去掉u' remove u'

python print 列印unicode string的時候 去掉u' remove u'

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: