(self, filename, encoding = None)
| 1551 | |
| 1552 | # load file and guess encoding |
| 1553 | def load_text (self, filename, encoding = None): |
| 1554 | content = None |
| 1555 | try: |
| 1556 | content = open(filename, 'rb').read() |
| 1557 | except: |
| 1558 | return None |
| 1559 | if content[:3] == b'\xef\xbb\xbf': |
| 1560 | text = content[3:].decode('utf-8') |
| 1561 | elif encoding is not None: |
| 1562 | text = content.decode(encoding, 'ignore') |
| 1563 | else: |
| 1564 | text = None |
| 1565 | guess = [sys.getdefaultencoding(), 'utf-8'] |
| 1566 | if sys.stdout and sys.stdout.encoding: |
| 1567 | guess.append(sys.stdout.encoding) |
| 1568 | for name in guess + ['gbk', 'ascii', 'latin1']: |
| 1569 | try: |
| 1570 | text = content.decode(name) |
| 1571 | break |
| 1572 | except: |
| 1573 | pass |
| 1574 | if text is None: |
| 1575 | text = content.decode('utf-8', 'ignore') |
| 1576 | return text |
| 1577 | |
| 1578 | # csv 读取,自动检测编码 |
| 1579 | def csv_load (self, filename, encoding = None): |
no test coverage detected