-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_file.py
More file actions
36 lines (27 loc) · 938 Bytes
/
print_file.py
File metadata and controls
36 lines (27 loc) · 938 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python3
# Sprax Lines 2016.07.25 Written with Python 3.5
'''print not necessarily ASCII text file to terminal'''
import sys
def read_file_lines(file):
''' read text file line by line '''
lines = []
with open(file, 'r') as text:
for line in text:
lines.append(line.rstrip())
my_print(line)
return lines
def print_lines(lines):
''' print items in a list on separate lines '''
for line in lines:
my_print(line)
def my_print(line):
''' print encoded string '''
print(line.encode("utf-8"))
def uprint(*objects, sep=' ', end='\n', file=sys.stdout):
''' print with encoding '''
enc = file.encoding
if enc == 'UTF-8':
print(*objects, sep=sep, end=end, file=file)
else:
f = lambda obj: str(obj).encode(enc, errors='backslashreplace').decode(enc)
print(*map(f, objects), sep=sep, end=end, file=file)