""" UTest.py - Test illustrating bug in codecs.open() with mode='U' """ import unittest import codecs, os FILE = 'C:\\test.txt' class UniversalNewline(unittest.TestCase): def roundTrip(self, text, encoding): f = codecs.open(FILE, 'w', encoding) f.write(text) f.close() f = codecs.open(FILE, 'rU', encoding) result = f.read() f.close() expected = text.replace(u'\r\n', u'\n').replace(u'\r', u'\n') self.assertEqual(expected, result) def testUtf16(self): self.roundTrip(u'hello\r\nworld\r\n', 'UTF-16') self.roundTrip(u'\u0a15\u0d15', 'UTF-16') def testUtf8(self): self.roundTrip(u'hello\r\nworld\r\n', 'UTF-8') self.roundTrip(u'\u0a15\u0d15', 'UTF-8') if __name__ == '__main__': unittest.main()

AltStyle によって変換されたページ (->オリジナル) /