forked from open-mmlab/mmocr
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add list_from_file and list_to_file (open-mmlab#226)
* Add list_from_file and list_to_file Signed-off-by: lizz <lizz@sensetime.com> * Add test list_to_file and list_from_file * more * Fix tests
- Loading branch information
Showing
15 changed files
with
261 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
def list_to_file(filename, lines): | ||
"""Write a list of strings to a text file. | ||
Args: | ||
filename (str): The output filename. It will be created/overwritten. | ||
lines (list(str)): Data to be written. | ||
""" | ||
with open(filename, 'w', encoding='utf-8') as fw: | ||
for line in lines: | ||
fw.write(f'{line}\n') | ||
|
||
|
||
def list_from_file(filename, encoding='utf-8'): | ||
"""Load a text file and parse the content as a list of strings. The | ||
trailing "\\r" and "\\n" of each line will be removed. | ||
Note: | ||
This will be replaced by mmcv's version after it supports encoding. | ||
Args: | ||
filename (str): Filename. | ||
encoding (str): Encoding used to open the file. Default utf-8. | ||
Returns: | ||
list[str]: A list of strings. | ||
""" | ||
item_list = [] | ||
with open(filename, 'r', encoding=encoding) as f: | ||
for line in f: | ||
item_list.append(line.rstrip('\n\r')) | ||
return item_list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import tempfile | ||
|
||
from mmocr.utils import list_from_file, list_to_file | ||
|
||
lists = [ | ||
[], | ||
[' '], | ||
['\t'], | ||
['a'], | ||
[1], | ||
[1.], | ||
['a', 'b'], | ||
['a', 1, 1.], | ||
[1, 1., 'a'], | ||
['啊', '啊啊'], | ||
['選択', 'noël', 'Информацией', 'ÄÆä'], | ||
] | ||
|
||
|
||
def test_list_to_file(): | ||
with tempfile.TemporaryDirectory() as tmpdirname: | ||
for i, lines in enumerate(lists): | ||
filename = f'{tmpdirname}/{i}.txt' | ||
list_to_file(filename, lines) | ||
lines2 = [ | ||
line.rstrip('\r\n') | ||
for line in open(filename, 'r', encoding='utf-8').readlines() | ||
] | ||
lines = list(map(str, lines)) | ||
assert len(lines) == len(lines2) | ||
assert all(line1 == line2 for line1, line2 in zip(lines, lines2)) | ||
|
||
|
||
def test_list_from_file(): | ||
with tempfile.TemporaryDirectory() as tmpdirname: | ||
for encoding in ['utf-8', 'utf-8-sig']: | ||
for lineend in ['\n', '\r\n']: | ||
for i, lines in enumerate(lists): | ||
filename = f'{tmpdirname}/{i}.txt' | ||
with open(filename, 'w', encoding=encoding) as f: | ||
f.writelines(f'{line}{lineend}' for line in lines) | ||
lines2 = list_from_file(filename, encoding=encoding) | ||
lines = list(map(str, lines)) | ||
assert len(lines) == len(lines2) | ||
assert all(line1 == line2 | ||
for line1, line2 in zip(lines, lines2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.