如何使用编码的字符串类型字符串

时间:2019-05-02 15:26:59

标签: python

我们在python中创建字节变量的通常方法是使用以下方法:

b = b'some text i do not care'

例如,编码为字节类型的中文字符串“鲁邦三世”为:

str_ch = "鲁邦三世"
encoded_str_ch = str_ch.encode("utf-8")
print(encoded_str_ch) # b'\xe9\xb2\x81\xe9\x82\xa6\xe4\xb8\x89\xe4\xb8\x96'

现在我是否有字符串:

s = '\xe9\xb2\x81\xe9\x82\xa6\xe4\xb8\x89\xe4\xb8\x96' 
# same with encoded_str_ch, but it's string type

我如何仅使用变量s而不是编码字符串'\ xe9 ... \ x96'来初始化字节变量

我尝试过

bytes(str_ch, encoding = "utf8")

但这是不正确的,与s仍然得到相同的结果

或者没有办法做到这一点...

1 个答案:

答案 0 :(得分:2)

因此,您有一个Unicode字符串,但是代码点实际上是UTF-8字节吗?通常,这意味着使用错误的编解码器对字符串进行了解码。下面的代码将代码点转换回字节,因为<Style x:Key="InlineDescriptionStyle" TargetType="{x:Type FrameworkElement}"> <Setter Property="TextBlock.FontSize" Value="10"/> <Setter Property="Opacity" Value="0.5"/> </Style> 是前256个代码点,并将1:1映射回字节:

class func deserialize(journalContent: String?) -> NSAttributedString {
    guard let journalContent = journalContent else {
        return NSAttributedString(string: "")
    }
    guard let attrStrData = journalContent.data(using: .unicode, allowLossyConversion: false) else {
        fatalError("Failed to construct data from journal content string")
    }
    let deserializationOptions: [NSAttributedString.DocumentReadingOptionKey: Any] = [
        .documentType: NSAttributedString.DocumentType.html,
        .characterEncoding: String.Encoding.utf8.rawValue
    ]
    guard let attributedString = try? NSAttributedString(data: attrStrData,
                                                         options: deserializationOptions,
                                                         documentAttributes: nil) else {
        fatalError("Failed to create attributed string from journal content \(journalContent)")
    }

    let journalContentAttributedString = NSMutableAttributedString()
    attributedString.enumerateAttribute(NSAttributedString.Key.font, in: NSMakeRange(0, attributedString.length), options: .init(rawValue: 0)) {
        (value, range, stop) in
        if let font = value as? UIFont {
            let resizedFont = font.withSize(font.pointSize * 0.75)
            let mutableAttributedSubstring = NSMutableAttributedString(attributedString: attributedString.attributedSubstring(from: range))
            let localRange = NSMakeRange(0, mutableAttributedSubstring.length)
            mutableAttributedSubstring.addAttribute(.font, value: resizedFont, range: localRange)
            mutableAttributedSubstring.removeAttribute(.paragraphStyle, range: localRange)
            mutableAttributedSubstring.removeAttribute(.kern, range: localRange)
            mutableAttributedSubstring.removeAttribute(.strokeWidth, range: localRange)
            mutableAttributedSubstring.removeAttribute(.strokeColor, range: localRange)
            journalContentAttributedString.append(mutableAttributedSubstring)
        }
    }
    return journalContentAttributedString
}

class func serialize(journalContent: NSAttributedString?) -> String {
    guard let journalContent = journalContent else {
        return ""
    }
    let documentAttributes = [
        NSAttributedString.DocumentAttributeKey.documentType: NSAttributedString.DocumentType.html
    ]
    guard let htmlData = try? journalContent.data(from: NSMakeRange(0, journalContent.length), documentAttributes:documentAttributes) else {
        return ""
    }
    guard let htmlString = NSString(data: htmlData, encoding:String.Encoding.utf8.rawValue) else {
        return ""
    }
    return htmlString as String
}