Skip to content
Snippets Groups Projects
Commit 596ab608 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

added encoding to read_lines and an unit test

parent ff2d71be
No related branches found
Tags v2.14.14
No related merge requests found
# v2.14.33
* added `encoding` to `read_lines`
__version__ = '2.14.33a1'
__version__ = '2.14.33a2'
......@@ -74,7 +74,8 @@ def _has_separator(path: str) -> bool:
return any(map(lambda x: x in path, SEPARATORS))
def read_lines(path: str, delete_empty_lines: bool = True) -> tp.List[str]:
def read_lines(path: str, delete_empty_lines: bool = True,
encoding: str = 'utf-8') -> tp.List[str]:
"""
Read lines from a particular file, removing end-of-line characters and optionally
empty lines. Additionally whitespaces (and end-of-line characters) will be removed
......@@ -82,9 +83,10 @@ def read_lines(path: str, delete_empty_lines: bool = True) -> tp.List[str]:
:param path: path of file to read
:param delete_empty_lines: set to False if empty lines are not to be removed
:param encoding: encoding to read the file with
:return: each line as a separate entry
"""
with open(path, 'r') as f_in:
with codecs.open(path, 'r', encoding) as f_in:
lines = [line.strip() for line in f_in.readlines()]
if delete_empty_lines:
lines = [line for line in lines if line]
......
......@@ -20,6 +20,15 @@ class TestFiles(unittest.TestCase):
lines = read_lines('LICENSE')
self.assertTrue(all(lines))
with open('test.tmp', 'w') as f_out:
f_out.write('''line1
line2
line3
''')
self.assertEqual(read_lines('test.tmp'), ['line1', 'line2', 'line3'])
def test_devnullfilelikeobject(self):
null = DevNullFilelikeObject()
self.assertEqual(null.write('ala'), 3)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment