def testDicts(self): """Test building dicts.""" with open(testlib.get_fixture_path("small.csv"), "r") as fd: reader = csv_reader.LazyCSVReader(fd, output_dicts=True) first_row = next(iter(reader)) self.assertEqual(dict(Name="Alice", Age="25", City="Zurich"), first_row)
def testCloseInDestructor(self): fd = open(testlib.get_fixture_path("names.txt"), "r") reader = csv_reader.LazyCSVReader(fd) for _ in reader: pass reader = None self.assertTrue(fd.closed)
def __call__(self, path, decode_header=False, delim=",", quote="\"", trim=True): fd = open(path, "r") # We don't close fd here, because repeated.lines is lazy and will read # on demand. The descriptor will be closed in the repeated value's # destructor. return csv_reader.LazyCSVReader(fd=fd, output_dicts=decode_header, delim=delim, quote=quote, trim=trim)
def testReading(self): """Test that reader reads the same lines as the File object.""" with open(testlib.get_fixture_path("small.csv"), "r") as fd: reader = csv_reader.LazyCSVReader(fd) self.assertValuesEqual(reader, repeated.meld(*small_csv.EXPECTED))
def run(self): with open(testlib.get_fixture_path("hygdata_v3.csv"), "r") as fd: for line in csv_reader.LazyCSVReader(fd, output_dicts=True): if float(line["dist"]) < 300 and len(line["proper"]) > 1: _ = dict(name=line["proper"], id=line["id"])