Python unicodecsv.reader() Examples
The following are 30
code examples of unicodecsv.reader().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
unicodecsv
, or try the search function
.
Example #1
Source File: japanese.py From messenger-maid-chan with MIT License | 6 votes |
def get_vocabulary(current_pos=1): """ get_vocabulary returns a single record of the current_pos line position current_pos: up to number of records """ vocabulary = {} with open(VOCABULARY_FILENAME, 'rb') as fobj: reader = csv.reader(fobj, delimiter=',', encoding='utf-8') num_of_lines = 0 for line in reader: num_of_lines += 1 if num_of_lines == current_pos: vocabulary = dict(list(zip(VOCABULARY_FIELDS, line))) break return vocabulary
Example #2
Source File: test.py From pyRevit with GNU General Public License v3.0 | 6 votes |
def test_read_dict_no_fieldnames(self): fd, name = tempfile.mkstemp() fileobj = os.fdopen(fd, "w+b") try: fileobj.write(b"f1,f2,f3\r\n1,2,abc\r\n") fileobj.seek(0) reader = csv.DictReader(fileobj) self.assertEqual(reader.fieldnames, ["f1", "f2", "f3"]) self.assertEqual(next(reader), {"f1": '1', "f2": '2', "f3": 'abc'}) finally: fileobj.close() os.unlink(name) # Two test cases to make sure existing ways of implicitly setting # fieldnames continue to work. Both arise from discussion in issue3436.
Example #3
Source File: space.py From epitran with MIT License | 6 votes |
def _load_space(self, space_names): segs = set() scripts = list(set([nm.split('-')[1] for nm in space_names])) punc_fns = ['punc-{}.csv'.format(sc) for sc in scripts] for punc_fn in punc_fns: punc_fn = os.path.join('data', 'space', punc_fn) punc_fn = pkg_resources.resource_filename(__name__, punc_fn) with open(punc_fn, 'rb') as f: reader = csv.reader(f, encoding='utf-8') for (mark,) in reader: segs.add(mark) for name in space_names: fn = os.path.join('data', 'space', name + '.csv') fn = pkg_resources.resource_filename(__name__, fn) with open(fn, 'rb') as f: reader = csv.reader(f, encoding='utf-8') for _, to_ in reader: for seg in self.epi.ft.ipa_segs(to_): segs.add(seg) enum = enumerate(sorted(list(segs))) return {seg: num for num, seg in enum}
Example #4
Source File: test.py From pyRevit with GNU General Public License v3.0 | 6 votes |
def test_space_dialect(self): class space(csv.excel): delimiter = " " quoting = csv.QUOTE_NONE escapechar = "\\" fd, name = tempfile.mkstemp() fileobj = os.fdopen(fd, "w+b") try: fileobj.write(b"abc def\nc1ccccc1 benzene\n") fileobj.seek(0) rdr = csv.reader(fileobj, dialect=space()) self.assertEqual(next(rdr), ["abc", "def"]) self.assertEqual(next(rdr), ["c1ccccc1", "benzene"]) finally: fileobj.close() os.unlink(name)
Example #5
Source File: featuretable.py From panphon with MIT License | 6 votes |
def _read_bases(self, fn, weights): fn = pkg_resources.resource_filename(__name__, fn) segments = [] with open(fn, 'rb') as f: reader = csv.reader(f, encoding='utf-8') header = next(reader) names = header[1:] for row in reader: ipa = unicodedata.normalize('NFD', row[0]) vals = [{'-': -1, '0': 0, '+': 1}[x] for x in row[1:]] vec = Segment(names, {n: v for (n, v) in zip(names, vals)}, weights=weights) segments.append((ipa, vec)) seg_dict = dict(segments) return segments, seg_dict, names
Example #6
Source File: test.py From pyRevit with GNU General Public License v3.0 | 6 votes |
def test_read_short(self): fd, name = tempfile.mkstemp() fileobj = os.fdopen(fd, "w+b") try: fileobj.write(b"1,2,abc,4,5,6\r\n1,2,abc\r\n") fileobj.seek(0) reader = csv.DictReader(fileobj, fieldnames="1 2 3 4 5 6".split(), restval="DEFAULT") self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', "4": '4', "5": '5', "6": '6'}) self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', "4": 'DEFAULT', "5": 'DEFAULT', "6": 'DEFAULT'}) finally: fileobj.close() os.unlink(name)
Example #7
Source File: _panphon.py From panphon with MIT License | 6 votes |
def _read_table(self, filename): """Read the data from data/ipa_all.csv into self.segments, a list of 2-tuples of unicode strings and sets of feature tuples and self.seg_dict, a dictionary mapping from unicode segments and sets of feature tuples. """ filename = pkg_resources.resource_filename( __name__, filename) segments = [] with open(filename, 'rb') as f: reader = csv.reader(f, encoding='utf-8') header = next(reader) names = header[1:] for row in reader: seg = row[0] vals = row[1:] specs = set(zip(vals, names)) segments.append((seg, specs)) seg_dict = dict(segments) return segments, seg_dict, names
Example #8
Source File: data_cleaner.py From data-cleaner with MIT License | 6 votes |
def _assert_no_duplicates(self, input_path, encoding, sep, quotechar): if input_path.endswith('.csv'): with open(input_path, 'rb') as csvfile: reader = unicodecsv.reader(csvfile, encoding=encoding, delimiter=sep, quotechar=quotechar) fields = next(reader, []) for col in fields: if fields.count(col) > 1: raise DuplicatedField(col) # TODO: Implementar chequeo de que no hay duplicados para XLSX elif input_path.endswith('.xlsx'): pass
Example #9
Source File: japanese.py From messenger-maid-chan with MIT License | 6 votes |
def get_kanji(level, current_pos=1): """ get_kanji returns a single record of the current_pos line position level: 1 - 4 (N1 to N4) current_pos: up to number of records """ kanji = {} with open(KANJI_FILENAMES[level], 'rb') as fobj: reader = csv.reader(fobj, delimiter=',', encoding='utf-8') num_of_lines = 0 for line in reader: num_of_lines += 1 if num_of_lines == current_pos: kanji = dict(list(zip(KANJI_FIELDS, line))) break return kanji
Example #10
Source File: test.py From pyRevit with GNU General Public License v3.0 | 5 votes |
def test_reader_dialect_attrs(self): self._test_dialect_attrs(csv.reader, [])
Example #11
Source File: test.py From pyRevit with GNU General Public License v3.0 | 5 votes |
def _read_test(self, input, expect, **kwargs): reader = csv.reader(input, **kwargs) result = list(reader) self.assertEqual(result, expect)
Example #12
Source File: test.py From pyRevit with GNU General Public License v3.0 | 5 votes |
def test_roundtrip_quoteed_newlines(self): fd, name = tempfile.mkstemp() fileobj = os.fdopen(fd, "w+b") try: writer = csv.writer(fileobj) self.assertRaises(TypeError, writer.writerows, None) rows = [['a\nb', 'b'], ['c', 'x\r\nd']] writer.writerows(rows) fileobj.seek(0) for i, row in enumerate(csv.reader(fileobj)): self.assertEqual(row, rows[i]) finally: fileobj.close() os.unlink(name)
Example #13
Source File: test.py From pyRevit with GNU General Public License v3.0 | 5 votes |
def test_register_kwargs(self): name = 'fedcba' csv.register_dialect(name, delimiter=';') try: self.assertNotEqual(csv.get_dialect(name).delimiter, '\t') self.assertEqual(list(csv.reader([b'X;Y;Z'], name)), [[u'X', u'Y', u'Z']]) finally: csv.unregister_dialect(name)
Example #14
Source File: test.py From pyRevit with GNU General Public License v3.0 | 5 votes |
def test_reader_kw_attrs(self): self._test_kw_attrs(csv.reader, [])
Example #15
Source File: test.py From pyRevit with GNU General Public License v3.0 | 5 votes |
def test_reader_attrs(self): self._test_default_attrs(csv.reader, [])
Example #16
Source File: test.py From pyRevit with GNU General Public License v3.0 | 5 votes |
def test_reader_arg_valid(self): self._test_arg_valid(csv.reader, [])
Example #17
Source File: test.py From pyRevit with GNU General Public License v3.0 | 5 votes |
def test_bad_dialect(self): # Unknown parameter self.assertRaises(TypeError, csv.reader, [], bad_attr=0) # Bad values self.assertRaises(TypeError, csv.reader, [], delimiter=None) self.assertRaises(TypeError, csv.reader, [], quoting=-1) self.assertRaises(TypeError, csv.reader, [], quoting=100)
Example #18
Source File: test.py From pyRevit with GNU General Public License v3.0 | 5 votes |
def readerAssertEqual(self, input, expected_result): fd, name = tempfile.mkstemp() fileobj = os.fdopen(fd, "w+b") try: fileobj.write(input) fileobj.seek(0) reader = csv.reader(fileobj, dialect=self.dialect) fields = list(reader) self.assertEqual(fields, expected_result) finally: fileobj.close() os.unlink(name)
Example #19
Source File: test.py From pyRevit with GNU General Public License v3.0 | 5 votes |
def test_read_dict_fieldnames_from_file(self): fd, name = tempfile.mkstemp() f = os.fdopen(fd, "w+b") try: f.write(b"f1,f2,f3\r\n1,2,abc\r\n") f.seek(0) reader = csv.DictReader(f, fieldnames=next(csv.reader(f))) self.assertEqual(reader.fieldnames, ["f1", "f2", "f3"]) self.assertEqual(next(reader), {"f1": '1', "f2": '2', "f3": 'abc'}) finally: f.close() os.unlink(name)
Example #20
Source File: xsampa.py From panphon with MIT License | 5 votes |
def read_xsampa_table(self): filename = os.path.join('data', 'ipa-xsampa.csv') filename = pkg_resources.resource_filename(__name__, filename) with open(filename, 'rb') as f: xs2ipa = {x[1]: x[0] for x in csv.reader(f, encoding='utf-8')} xs = sorted(xs2ipa.keys(), key=len, reverse=True) xs_regex = re.compile('|'.join(list(map(re.escape, xs)))) return xs_regex, xs2ipa
Example #21
Source File: test.py From pyRevit with GNU General Public License v3.0 | 5 votes |
def test_read_dict_fieldnames_chain(self): import itertools fd, name = tempfile.mkstemp() f = os.fdopen(fd, "w+b") try: f.write(b"f1,f2,f3\r\n1,2,abc\r\n") f.seek(0) reader = csv.DictReader(f) first = next(reader) for row in itertools.chain([first], reader): self.assertEqual(reader.fieldnames, ["f1", "f2", "f3"]) self.assertEqual(row, {"f1": '1', "f2": '2', "f3": 'abc'}) finally: f.close() os.unlink(name)
Example #22
Source File: test.py From pyRevit with GNU General Public License v3.0 | 5 votes |
def test_read_long(self): fd, name = tempfile.mkstemp() fileobj = os.fdopen(fd, "w+b") try: fileobj.write(b"1,2,abc,4,5,6\r\n") fileobj.seek(0) reader = csv.DictReader(fileobj, fieldnames=["f1", "f2"]) self.assertEqual(next(reader), {"f1": '1', "f2": '2', None: ["abc", "4", "5", "6"]}) finally: fileobj.close() os.unlink(name)
Example #23
Source File: test.py From pyRevit with GNU General Public License v3.0 | 5 votes |
def test_read_long_with_rest(self): fd, name = tempfile.mkstemp() fileobj = os.fdopen(fd, "w+b") try: fileobj.write(b"1,2,abc,4,5,6\r\n") fileobj.seek(0) reader = csv.DictReader(fileobj, fieldnames=["f1", "f2"], restkey="_rest") self.assertEqual(next(reader), {"f1": '1', "f2": '2', "_rest": ["abc", "4", "5", "6"]}) finally: fileobj.close() os.unlink(name)
Example #24
Source File: test.py From pyRevit with GNU General Public License v3.0 | 5 votes |
def test_read_multi(self): sample = [ b'2147483648,43.0e12,17,abc,def\r\n', b'147483648,43.0e2,17,abc,def\r\n', b'47483648,43.0,170,abc,def\r\n' ] reader = csv.DictReader(sample, fieldnames="i1 float i2 s1 s2".split()) self.assertEqual(next(reader), {"i1": '2147483648', "float": '43.0e12', "i2": '17', "s1": 'abc', "s2": 'def'})
Example #25
Source File: test.py From pyRevit with GNU General Public License v3.0 | 5 votes |
def test_read_with_blanks(self): reader = csv.DictReader([b"1,2,abc,4,5,6\r\n", b"\r\n", b"1,2,abc,4,5,6\r\n"], fieldnames="1 2 3 4 5 6".split()) self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', "4": '4', "5": '5', "6": '6'}) self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', "4": '4', "5": '5', "6": '6'})
Example #26
Source File: test.py From pyRevit with GNU General Public License v3.0 | 5 votes |
def test_read_semi_sep(self): reader = csv.DictReader([b"1;2;abc;4;5;6\r\n"], fieldnames="1 2 3 4 5 6".split(), delimiter=';') self.assertEqual(next(reader), {"1": '1', "2": '2', "3": 'abc', "4": '4', "5": '5', "6": '6'})
Example #27
Source File: test.py From pyRevit with GNU General Public License v3.0 | 5 votes |
def test_unicode_read(self): f = EncodedFile(BytesIO((u"Martin von Löwis," u"Marc André Lemburg," u"Guido van Rossum," u"François Pinard\r\n").encode('iso-8859-1')), data_encoding='iso-8859-1') reader = csv.reader(f, encoding='iso-8859-1') self.assertEqual(list(reader), [[u"Martin von Löwis", u"Marc André Lemburg", u"Guido van Rossum", u"François Pinard"]])
Example #28
Source File: test.py From pyRevit with GNU General Public License v3.0 | 5 votes |
def test_decode_error_dictreader(self): """Make sure the error-handling mode is obeyed on DictReaders.""" file = EncodedFile(BytesIO(u'name,height,weight\nLöwis,2,3'.encode('iso-8859-1')), data_encoding='iso-8859-1') reader = csv.DictReader(file, encoding='ascii', errors='ignore') self.assertEqual(list(reader)[0]['name'], 'Lwis')
Example #29
Source File: convert.py From csvtotable with MIT License | 5 votes |
def convert(input_file_name, **kwargs): """Convert CSV file to HTML table""" delimiter = kwargs["delimiter"] or "," quotechar = kwargs["quotechar"] or "|" if six.PY2: delimiter = delimiter.encode("utf-8") quotechar = quotechar.encode("utf-8") # Read CSV and form a header and rows list with open(input_file_name, "rb") as input_file: reader = csv.reader(input_file, encoding="utf-8", delimiter=delimiter, quotechar=quotechar) csv_headers = [] if not kwargs.get("no_header"): # Read header from first line csv_headers = next(reader) csv_rows = [row for row in reader if row] # Set default column name if header is not present if not csv_headers and len(csv_rows) > 0: end = len(csv_rows[0]) + 1 csv_headers = ["Column {}".format(n) for n in range(1, end)] # Render csv to HTML html = render_template(csv_headers, csv_rows, **kwargs) # Freeze all JS files in template return freeze_js(html)
Example #30
Source File: test_actions.py From edx-enterprise with GNU Affero General Public License v3.0 | 5 votes |
def _assert_correct_csv(self, actual_csv, expected_rows): """ Asserts that CSV file ``actual_csv`` contains ``expected_rows`` """ reader = unicodecsv.reader(actual_csv.getvalue().splitlines(), encoding="utf-8") # preprocess expected - convert everything to strings expected_rows = [ [str(item) for item in row] for row in expected_rows ] actual_rows = list(reader) self.assertEqual(actual_rows, expected_rows)