66from pystreamapi .loaders .__lazy_file_iterable import LazyFileIterable
77
88
9- def csv (file_path : str , delimiter = ',' , encoding = "utf-8" ) -> LazyFileIterable :
9+ def csv (file_path : str , cast_types = True , delimiter = ',' , encoding = "utf-8" ) -> LazyFileIterable :
1010 """
1111 Loads a CSV file and converts it into a list of namedtuples.
1212
1313 Returns:
1414 list: A list of namedtuples, where each namedtuple represents a row in the CSV.
15+ :param cast_types: Set as False to disable casting of values to int, bool or float.
1516 :param encoding: The encoding of the CSV file.
1617 :param file_path: The path to the CSV file.
1718 :param delimiter: The delimiter used in the CSV file.
1819 """
1920 file_path = __validate_path (file_path )
20- return LazyFileIterable (lambda : __load_csv (file_path , delimiter , encoding ))
21+ return LazyFileIterable (lambda : __load_csv (file_path , cast_types , delimiter , encoding ))
2122
2223
23- def __load_csv (file_path , delimiter , encoding ):
24+ def __load_csv (file_path , cast , delimiter , encoding ):
2425 """Load a CSV file and convert it into a list of namedtuples"""
2526 # skipcq: PTC-W6004
2627 with open (file_path , mode = 'r' , newline = '' , encoding = encoding ) as csvfile :
@@ -29,8 +30,10 @@ def __load_csv(file_path, delimiter, encoding):
2930 # Create a namedtuple type, casting the header values to int or float if possible
3031 Row = namedtuple ('Row' , list (next (csvreader , [])))
3132
33+ mapper = __try_cast if cast else lambda x : x
34+
3235 # Process the data, casting values to int or float if possible
33- data = [Row (* [__try_cast (value ) for value in row ]) for row in csvreader ]
36+ data = [Row (* [mapper (value ) for value in row ]) for row in csvreader ]
3437 return data
3538
3639
0 commit comments