Python2 Unicode

#1 英文str与unicode 内容相同 但是类型不同 

test_type

#2 中文str与unicode 内容与类型都不同

test_type

#3 open只能按照str读

test_open_str_file

test_open_unicode_file

 

#4 codecs只能按照unicode读

test_codecs_str_file

test_codecs_unicode_file

 

#5 open只能写str内容 (英文两种皆可)

test_file_can_not_write_unicode

#6 codecs只能写unicode内容 (英文两种皆可)

test_codecs_can_not_write_str


# -*- encoding: utf-8 -*-
import codecs
import unittest
import os

FAILED_CODECS = "test_codecs_can_not_write_str.txt"
FAILED_FILE = "test_file_can_not_write_unicode.txt"
STR_FILE = "str.txt"
UNICODE_FILE = "unicode.txt"


def to_unicode(unicode_or_str):
    if isinstance(unicode_or_str, str):
        value = unicode_or_str.decode('utf-8')
    else:
        value = unicode_or_str
    return value


def to_str(unicode_or_str):
    if isinstance(unicode_or_str, unicode):
        value = unicode_or_str.encode('utf-8')
    else:
        value = unicode_or_str
    return value


class Unicode(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        with open(STR_FILE, "w") as outfile:
            outfile.write("你好")
        with codecs.open(UNICODE_FILE, "w", "utf-8") as outfile:
            outfile.write(u"你好")

    @classmethod
    def tearDownClass(cls):
        os.remove(FAILED_CODECS)
        os.remove(FAILED_FILE)
        os.remove(STR_FILE)
        os.remove(UNICODE_FILE)

    def test_open_str_file(self):
        with open(STR_FILE, 'r') as infile:
            line = infile.read()
            self.assertIsInstance(line, str)
            self.assertEqual("你好", line)

    def test_open_unicode_file(self):
        with open(UNICODE_FILE, 'r') as infile:
            line = infile.read()
            self.assertIsInstance(line, str)
            self.assertEqual("你好", line)

    def test_codecs_str_file(self):
        with codecs.open(STR_FILE, "r", "utf-8") as infile:
            line = infile.read()
            self.assertIsInstance(line, unicode)
            self.assertEqual(u"你好", line)

    def test_codecs_unicode_file(self):
        with codecs.open(UNICODE_FILE, "r", "utf-8") as infile:
            line = infile.read()
            self.assertIsInstance(line, unicode)
            self.assertEqual(u"你好", line)

    def test_file_can_not_write_unicode(self):
        outfile = open(FAILED_FILE, "w")
        outfile.write(u"hello")
        with self.assertRaises(UnicodeEncodeError):
            outfile.write(u"你好")

    def test_codecs_can_not_write_str(self):
        outfile = codecs.open(FAILED_CODECS, "w", "utf-8")
        outfile.write("hello")
        with self.assertRaises(UnicodeDecodeError):
            outfile.write("你好")

    def test_type(self):
        self.assertIsInstance("你好", str)
        self.assertIsInstance(u"你好", unicode)
        self.assertIsInstance(to_unicode("你好"), unicode)
        self.assertIsInstance(to_str(u"你好"), str)
        self.assertIsInstance(to_str("你好"), str)
        self.assertIsInstance(to_unicode(u"你好"), unicode)

        self.assertIsInstance("hello", str)
        self.assertIsInstance(u"hello", unicode)
        self.assertIsInstance(to_unicode("hello"), unicode)
        self.assertIsInstance(to_str(u"hello"), str)
        self.assertIsInstance(to_str("hello"), str)
        self.assertIsInstance(to_unicode(u"hello"), unicode)

    def test_str_VS_str(self):
        self.assertEqual("你好", "你好")

    def test_unicode_VS_unicode(self):
        self.assertEqual(u"你好", u"你好")

    def test_str_VS_unicode1(self):
        with self.assertRaises(UnicodeDecodeError):
            print "你好" in u"你好"

    def test_str_VS_unicode2(self):
        self.assertEqual("hello", u"hello")


if __name__ == '__main__':
    unittest.main()


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值