如何将转义的Unicode字符串转换为Unicode字符

时间:2016-10-09 15:20:36

标签: swift3

我有一个在文本编辑器中看起来像这样的plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <array>
        <string>\u{1f604}</string>  
        <string>7\u{20e3}</string>  
        <string>\u{1f1ef}\u{1f1f5}</string> 
        ...     
    </array>
</plist>

当我得到并打印字符串时,它打印如下:

\u{1f604}

我怀疑该字符串以某种方式无形地转义为\\u{1f604}

如何转换字符串,以便打印代表字符

2 个答案:

答案 0 :(得分:1)

首先,你需要知道Xcode的plist编辑器可以直接包含emojis:

public EffetMatrix(Context context) {
    this(context, null);
}

public EffetMatrix(Context context, AttributeSet attrs) {
    super(context, attrs);  
}

但是,如果你已经有一些plist文件的转义版本,你可能需要转换它。使用import xlwt import numpy as np from odbAccess import * odb = visualization.openOdb('C:/Users/'+Name+'.odb') frame=odb.steps['AStep'].frames[-1] dispNode = odb.rootAssembly.nodeSets['UPPER'] TEMP= frame.fieldOutputs['S'].getSubset(region=dispNode).values COORD= frame.fieldOutputs['COORD'].getSubset(region=dispNode).values sortie = open('Resu_PE_maillage.txt', 'w') book = xlwt.Workbook() sh = book.add_sheet("sheet") col1_name = 'X Coordinate' col2_name = 'Y Coordinate' col3_name = 'Z Coordinate' col4_name = 'Stress' sh.write(0, 0, col1_name) sh.write(0, 1, col2_name) sh.write(0, 2, col3_name) sh.write(0, 3, col4_name) for NT in range(1,len(TEMP)): sh.row(NT).set_cell_number(0,COORD[NT].data[0]) sh.row(NT).set_cell_number(1,COORD[NT].data[1]) sh.row(NT).set_cell_number(2,COORD[NT].data[2]) sh.row(NT).set_cell_number(3,TEMP[NT].data[1]) if i<10: book.save('C:/Users/'+'00'+str(i)+'.xls') if 9<i<100: book.save('C:/Users/'+'0'+str(i)+'.xls') if 99<i<1000: book.save('C:/Users/'+str(i)+'.xls') ,您可以编写如下内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>emojis</key>
    <array>
        <string></string>
        <string></string>
        <string>&#x1F601;</string><!-- or you can use numeric reference of XML, in XML editor -->
    </array>
</dict>
</plist>

答案 1 :(得分:0)

I think you'll have to parse the string manually:

func toCharacter(_ string: String) -> Character? {
    guard
        let start = string.characters.index(of: "{"),
        let end = string.characters.index(of: "}"),
        start < end
        else { return nil }

    let range = string.index(after: start) ..< end

    guard
        let number = Int(string[range], radix: 16),
        let scalar = UnicodeScalar(number)
        else { return nil }

    return Character(scalar)
}

toCharacter("\\u{1f604}") // ""