本文整理汇总了Python中fileinput.hook_encoded方法的典型用法代码示例。如果您正苦于以下问题:Python fileinput.hook_encoded方法的具体用法?Python fileinput.hook_encoded怎么用?Python fileinput.hook_encoded使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fileinput
的用法示例。
在下文中一共展示了fileinput.hook_encoded方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_file_opening_hook
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import hook_encoded [as 别名]
def test_file_opening_hook(self):
try:
# cannot use openhook and inplace mode
fi = FileInput(inplace=1, openhook=lambda f,m: None)
self.fail("FileInput should raise if both inplace "
"and openhook arguments are given")
except ValueError:
pass
try:
fi = FileInput(openhook=1)
self.fail("FileInput should check openhook for being callable")
except ValueError:
pass
try:
# UTF-7 is a convenient, seldom used encoding
t1 = writeTmp(1, ['+AEE-\n+AEI-'], mode="wb")
fi = FileInput(files=t1, openhook=hook_encoded("utf-7"))
lines = list(fi)
self.assertEqual(lines, [u'A\n', u'B'])
finally:
remove_tempfiles(t1)
示例2: test_readline
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import hook_encoded [as 别名]
def test_readline(self):
with open(TESTFN, 'wb') as f:
f.write('A\nB\r\nC\r')
# Fill TextIOWrapper buffer.
f.write('123456789\n' * 1000)
# Issue #20501: readline() shouldn't read whole file.
f.write('\x80')
self.addCleanup(safe_unlink, TESTFN)
fi = FileInput(files=TESTFN, openhook=hook_encoded('ascii'))
# The most likely failure is a UnicodeDecodeError due to the entire
# file being read when it shouldn't have been.
self.assertEqual(fi.readline(), u'A\n')
self.assertEqual(fi.readline(), u'B\r\n')
self.assertEqual(fi.readline(), u'C\r')
with self.assertRaises(UnicodeDecodeError):
# Read to the end of file.
list(fi)
fi.close()
示例3: test_modes
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import hook_encoded [as 别名]
def test_modes(self):
with open(TESTFN, 'wb') as f:
# UTF-7 is a convenient, seldom used encoding
f.write('A\nB\r\nC\rD+IKw-')
self.addCleanup(safe_unlink, TESTFN)
def check(mode, expected_lines):
fi = FileInput(files=TESTFN, mode=mode,
openhook=hook_encoded('utf-7'))
lines = list(fi)
fi.close()
self.assertEqual(lines, expected_lines)
check('r', [u'A\n', u'B\r\n', u'C\r', u'D\u20ac'])
check('rU', [u'A\n', u'B\r\n', u'C\r', u'D\u20ac'])
check('U', [u'A\n', u'B\r\n', u'C\r', u'D\u20ac'])
check('rb', [u'A\n', u'B\r\n', u'C\r', u'D\u20ac'])
示例4: test_file_opening_hook
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import hook_encoded [as 别名]
def test_file_opening_hook(self):
try:
# cannot use openhook and inplace mode
fi = FileInput(inplace=1, openhook=lambda f,m: None)
self.fail("FileInput should raise if both inplace "
"and openhook arguments are given")
except ValueError:
pass
try:
fi = FileInput(openhook=1)
self.fail("FileInput should check openhook for being callable")
except ValueError:
pass
try:
t1 = writeTmp(1, ["A\nB"], mode="wb")
fi = FileInput(files=t1, openhook=hook_encoded("rot13"))
lines = list(fi)
self.assertEqual(lines, ["N\n", "O"])
finally:
remove_tempfiles(t1)
示例5: test_readline
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import hook_encoded [as 别名]
def test_readline(self):
with open(TESTFN, 'wb') as f:
f.write('A\nB\r\nC\r')
# Fill TextIOWrapper buffer.
f.write('123456789\n' * 1000)
# Issue #20501: readline() shouldn't read whole file.
f.write('\x80')
self.addCleanup(safe_unlink, TESTFN)
fi = FileInput(files=TESTFN, openhook=hook_encoded('ascii'), bufsize=8)
# The most likely failure is a UnicodeDecodeError due to the entire
# file being read when it shouldn't have been.
self.assertEqual(fi.readline(), u'A\n')
self.assertEqual(fi.readline(), u'B\r\n')
self.assertEqual(fi.readline(), u'C\r')
with self.assertRaises(UnicodeDecodeError):
# Read to the end of file.
list(fi)
fi.close()
示例6: download_with_info_file
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import hook_encoded [as 别名]
def download_with_info_file(self, info_filename):
with contextlib.closing(fileinput.FileInput(
[info_filename], mode='r',
openhook=fileinput.hook_encoded('utf-8'))) as f:
# FileInput doesn't have a read method, we can't call json.load
info = self.filter_requested_info(json.loads('\n'.join(f)))
try:
self.process_ie_result(info, download=True)
except DownloadError:
webpage_url = info.get('webpage_url')
if webpage_url is not None:
self.report_warning('The info failed to download, trying with "%s"' % webpage_url)
return self.download([webpage_url])
else:
raise
return self._download_retcode
示例7: test_readline
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import hook_encoded [as 别名]
def test_readline(self):
with open(TESTFN, 'wb') as f:
f.write(b'A\nB\r\nC\r')
# Fill TextIOWrapper buffer.
f.write(b'123456789\n' * 1000)
# Issue #20501: readline() shouldn't read whole file.
f.write(b'\x80')
self.addCleanup(safe_unlink, TESTFN)
with FileInput(files=TESTFN,
openhook=hook_encoded('ascii'), bufsize=8) as fi:
try:
self.assertEqual(fi.readline(), 'A\n')
self.assertEqual(fi.readline(), 'B\n')
self.assertEqual(fi.readline(), 'C\n')
except UnicodeDecodeError:
self.fail('Read to end of file')
with self.assertRaises(UnicodeDecodeError):
# Read to the end of file.
list(fi)
self.assertEqual(fi.readline(), '')
self.assertEqual(fi.readline(), '')
示例8: test
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import hook_encoded [as 别名]
def test(self):
encoding = object()
result = fileinput.hook_encoded(encoding)
fake_open = InvocationRecorder()
original_open = builtins.open
builtins.open = fake_open
try:
filename = object()
mode = object()
open_result = result(filename, mode)
finally:
builtins.open = original_open
self.assertEqual(fake_open.invocation_count, 1)
args, kwargs = fake_open.last_invocation
self.assertIs(args[0], filename)
self.assertIs(args[1], mode)
self.assertIs(kwargs.pop('encoding'), encoding)
self.assertFalse(kwargs)
示例9: test_modes
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import hook_encoded [as 别名]
def test_modes(self):
with open(TESTFN, 'wb') as f:
# UTF-7 is a convenient, seldom used encoding
f.write(b'A\nB\r\nC\rD+IKw-')
self.addCleanup(safe_unlink, TESTFN)
def check(mode, expected_lines):
with FileInput(files=TESTFN, mode=mode,
openhook=hook_encoded('utf-7')) as fi:
lines = list(fi)
self.assertEqual(lines, expected_lines)
check('r', ['A\n', 'B\n', 'C\n', 'D\u20ac'])
with self.assertWarns(DeprecationWarning):
check('rU', ['A\n', 'B\n', 'C\n', 'D\u20ac'])
with self.assertWarns(DeprecationWarning):
check('U', ['A\n', 'B\n', 'C\n', 'D\u20ac'])
with self.assertRaises(ValueError):
check('rb', ['A\n', 'B\r\n', 'C\r', 'D\u20ac'])
示例10: main
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import hook_encoded [as 别名]
def main(args):
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("files", action="store", nargs="*", help="files to print")
ns = p.parse_args(args)
status = 0
fileinput.close() # in case it is not closed
try:
for line in fileinput.input(ns.files, openhook=fileinput.hook_encoded("utf-8")):
print(filter_non_printable(line), end='')
except Exception as e:
print('cat: %s' % str(e))
status = 1
finally:
fileinput.close()
sys.exit(status)
示例11: more
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import hook_encoded [as 别名]
def more(filenames, pagesize=10, clear=False, fmt='{line}'):
'''Display content of filenames pagesize lines at a time (cleared if specified) with format fmt for each output line'''
fileinput.close() # in case still open
try:
pageno = 1
if clear:
clear_screen()
for line in fileinput.input(filenames, openhook=fileinput.hook_encoded("utf-8")):
lineno, filename, filelineno = fileinput.lineno(), fileinput.filename(), fileinput.filelineno()
print(fmt.format(**locals()), end='')
if pagesize and lineno % pagesize == 0:
console.alert('Abort or continue', filename, 'Next page') # TODO: use less intrusive mechanism than alert
pageno += 1
if clear:
clear_screen()
finally:
fileinput.close()
# --- main
示例12: test_readline
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import hook_encoded [as 别名]
def test_readline(self):
with open(TESTFN, 'wb') as f:
f.write(b'A\nB\r\nC\r')
# Fill TextIOWrapper buffer.
f.write(b'123456789\n' * 1000)
# Issue #20501: readline() shouldn't read whole file.
f.write(b'\x80')
self.addCleanup(safe_unlink, TESTFN)
with FileInput(files=TESTFN,
openhook=hook_encoded('ascii')) as fi:
try:
self.assertEqual(fi.readline(), 'A\n')
self.assertEqual(fi.readline(), 'B\n')
self.assertEqual(fi.readline(), 'C\n')
except UnicodeDecodeError:
self.fail('Read to end of file')
with self.assertRaises(UnicodeDecodeError):
# Read to the end of file.
list(fi)
self.assertEqual(fi.readline(), '')
self.assertEqual(fi.readline(), '')
示例13: handle
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import hook_encoded [as 别名]
def handle(self, *args, **options):
count = 0
UserModel = get_user_model()
for line in fileinput(args, openhook=hook_encoded("utf-8")):
try:
username, password = line.rstrip("\r\n").split(":", 1)
except ValueError:
raise CommandError(
"Invalid input provided. "
"Format is 'username:password', one per line."
)
try:
user = UserModel._default_manager.get(
**{UserModel.USERNAME_FIELD: username}
)
except UserModel.DoesNotExist:
raise CommandError("User '%s' does not exist." % username)
user.set_password(password)
user.save()
count += 1
return "%d password(s) successfully changed." % count
示例14: buffered_read
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import hook_encoded [as 别名]
def buffered_read(input, buffer_size):
buffer = []
with fileinput.input(files=[input], openhook=fileinput.hook_encoded("utf-8")) as h:
for src_str in h:
buffer.append(src_str.strip())
if len(buffer) >= buffer_size:
yield buffer
buffer = []
if len(buffer) > 0:
yield buffer
示例15: open_csv
# 需要导入模块: import fileinput [as 别名]
# 或者: from fileinput import hook_encoded [as 别名]
def open_csv(self, mode='r', newline=''):
f = fileinput.input(files=(self.filepath), openhook=fileinput.hook_encoded("utf-8-sig"))
try:
yield f
finally:
f.close()