Unverified Commit 50537ddf authored by lizz's avatar lizz Committed by GitHub
Browse files

Imporve windows support for list_from_file (#1043)


Signed-off-by: default avatarlizz <lizz@sensetime.com>
parent b028a1f9
# Copyright (c) Open-MMLab. All rights reserved. # Copyright (c) Open-MMLab. All rights reserved.
def list_from_file(filename, prefix='', offset=0, max_num=0): def list_from_file(filename, prefix='', offset=0, max_num=0, encoding='utf-8'):
"""Load a text file and parse the content as a list of strings. """Load a text file and parse the content as a list of strings.
Args: Args:
...@@ -8,19 +8,20 @@ def list_from_file(filename, prefix='', offset=0, max_num=0): ...@@ -8,19 +8,20 @@ def list_from_file(filename, prefix='', offset=0, max_num=0):
offset (int): The offset of lines. offset (int): The offset of lines.
max_num (int): The maximum number of lines to be read, max_num (int): The maximum number of lines to be read,
zeros and negatives mean no limitation. zeros and negatives mean no limitation.
encoding (str): Encoding used to open the file. Default utf-8.
Returns: Returns:
list[str]: A list of strings. list[str]: A list of strings.
""" """
cnt = 0 cnt = 0
item_list = [] item_list = []
with open(filename, 'r') as f: with open(filename, 'r', encoding=encoding) as f:
for _ in range(offset): for _ in range(offset):
f.readline() f.readline()
for line in f: for line in f:
if max_num > 0 and cnt >= max_num: if 0 < max_num <= cnt:
break break
item_list.append(prefix + line.rstrip('\n')) item_list.append(prefix + line.rstrip('\n\r'))
cnt += 1 cnt += 1
return item_list return item_list
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment