def testUnicode(self): reader = csv.Reader("wąwóz,źdźbło\ngrzęda,wątły\ndźwig,ścieżka") self.assertEqual(list(reader), [ ["wąwóz", "źdźbło"], ["grzęda", "wątły"], ["dźwig", "ścieżka"], ])
def testMultipleRows(self): reader = csv.Reader("foo,quux\nbar,norf\nbaz,thud") self.assertEqual(list(reader), [ ["foo", "quux"], ["bar", "norf"], ["baz", "thud"], ])
def ImportFile(store, filename, start): """Import hashes from 'filename' into 'store'.""" with io.open(filename, "r") as fp: reader = csv.Reader(fp.read()) i = 0 current_row = None product_code_list = [] op_system_code_list = [] for row in reader: # Skip first row. i += 1 if i and i % 5000 == 0: data_store.DB.Flush() print("Imported %d hashes" % i) if i > 1: if len(row) != 8: continue try: if i < start: continue if current_row: if current_row[0] == row[0]: # Same hash, add product/system product_code_list.append(int(row[5])) op_system_code_list.append(row[6]) continue # Fall through and add current row. else: # First row. current_row = row product_code_list = [int(row[5])] op_system_code_list = [row[6]] continue _ImportRow(store, current_row, product_code_list, op_system_code_list) # Set new hash. current_row = row product_code_list = [int(row[5])] op_system_code_list = [row[6]] except Exception as e: # pylint: disable=broad-except print("Failed at %d with %s" % (i, str(e))) return i - 1 if current_row: _ImportRow(store, current_row, product_code_list, op_system_code_list) return i
def testDefaultQuotechar(self): reader = csv.Reader("foo,\"bar, baz, quux\",norf,\"thud\"") self.assertEqual(list(reader), [["foo", "bar, baz, quux", "norf", "thud"]])
def testMultipleUsages(self): reader = csv.Reader("foo") self.assertEqual(list(reader), [["foo"]]) self.assertEqual(list(reader), [["foo"]])
def testCustomDelimiter(self): reader = csv.Reader("foo|bar|baz", delimiter="|") self.assertEqual(list(reader), [["foo", "bar", "baz"]])
def testSingleRow(self): reader = csv.Reader("foo,bar,baz") self.assertEqual(list(reader), [["foo", "bar", "baz"]])
def testEmpty(self): reader = csv.Reader("") self.assertEqual(list(reader), [])