mirror of
https://gitee.com/fastnlp/fastNLP.git
synced 2024-12-05 05:38:31 +08:00
loader-pr
This commit is contained in:
parent
04a54df226
commit
53bcc0b26a
@ -2,6 +2,7 @@
|
|||||||
此模块用于给其它模块提供读取文件的函数,没有为用户提供 API
|
此模块用于给其它模块提供读取文件的函数,没有为用户提供 API
|
||||||
"""
|
"""
|
||||||
import json
|
import json
|
||||||
|
import csv
|
||||||
|
|
||||||
|
|
||||||
def _read_csv(path, encoding='utf-8', headers=None, sep=',', dropna=True):
|
def _read_csv(path, encoding='utf-8', headers=None, sep=',', dropna=True):
|
||||||
@ -16,17 +17,16 @@ def _read_csv(path, encoding='utf-8', headers=None, sep=',', dropna=True):
|
|||||||
:if False, raise ValueError when reading invalid data. default: True
|
:if False, raise ValueError when reading invalid data. default: True
|
||||||
:return: generator, every time yield (line number, csv item)
|
:return: generator, every time yield (line number, csv item)
|
||||||
"""
|
"""
|
||||||
with open(path, 'r', encoding=encoding) as f:
|
f = csv.reader(open(path, encoding=encoding), delimiter=sep)
|
||||||
start_idx = 0
|
start_idx = 0
|
||||||
if headers is None:
|
if headers is None:
|
||||||
headers = f.readline().rstrip('\r\n')
|
headers = next(f)
|
||||||
headers = headers.split(sep)
|
|
||||||
start_idx += 1
|
start_idx += 1
|
||||||
elif not isinstance(headers, (list, tuple)):
|
elif not isinstance(headers, (list, tuple)):
|
||||||
raise TypeError("headers should be list or tuple, not {}." \
|
raise TypeError("headers should be list or tuple, not {}." \
|
||||||
.format(type(headers)))
|
.format(type(headers)))
|
||||||
for line_idx, line in enumerate(f, start_idx):
|
for line_idx, line in enumerate(f, start_idx):
|
||||||
contents = line.rstrip('\r\n').split(sep)
|
contents = line
|
||||||
if len(contents) != len(headers):
|
if len(contents) != len(headers):
|
||||||
if dropna:
|
if dropna:
|
||||||
continue
|
continue
|
||||||
|
6
fastNLP/io/test.csv
Normal file
6
fastNLP/io/test.csv
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
a b
|
||||||
|
1 "Contrary to other reviews, I have zero complaints about the service or the prices. I have been getting tire service here for the past 5 years now, and compared to my experience with places like Pep Boys, these guys are experienced and know what they're doing. \nAlso, this is one place that I do not feel like I am being taken advantage of, just because of my gender. Other auto mechanics have been notorious for capitalizing on my ignorance of cars, and have sucked my bank account dry. But here, my service and road coverage has all been well explained - and let up to me to decide. \nAnd they just renovated the waiting room. It looks a lot better than it did in previous years."
|
||||||
|
2 "Last summer I had an appointment to get new tires and had to wait a super long time. I also went in this week for them to fix a minor problem with a tire they put on. They \""fixed\"" it for free, and the very next morning I had the same issue. I called to complain, and the \""manager\"" didn't even apologize!!! So frustrated. Never going back. They seem overpriced, too."
|
||||||
|
3 "Friendly staff, same starbucks fair you get anywhere else. Sometimes the lines can get long."
|
||||||
|
4 "The food is good. Unfortunately the service is very hit or miss. The main issue seems to be with the kitchen, the waiters and waitresses are often very apologetic for the long waits and it's pretty obvious that some of them avoid the tables after taking the initial order to avoid hearing complaints."
|
||||||
|
5 "Even when we didn't have a car Filene's Basement was worth the bus trip to the Waterfront. I always find something (usually I find 3-4 things and spend about $60) and better still, I am always still wearing the clothes and shoes 3 months later. \n\nI kind of suspect this is the best shopping in Pittsburgh; it's much better than the usual department stores, better than Marshall's and TJ Maxx and better than the Saks downtown, even when it has a sale. Selection, bargains AND quality.\n\nI like this Filene's better than Gabriel Brothers, which are harder to get to. Gabriel Brothers are a real discount shopper's challenge and I'm afraid I didn't live in Pittsburgh long enough to develop the necessary skills . . . Filene's was still up and running in June 2007 when I left town."
|
Can't render this file because it contains an unexpected character in line 2 and column 5.
|
Loading…
Reference in New Issue
Block a user