def openGenericCSV(path): """Open the CSV at the specified path. Try to guess the type of CSV. Args: path (str): CSV filepath to be opened. Returns: dict containing 'headers' list of headers and 'rows' list of rows. """ with open(path, 'r') as inFile: # Use sniffer to guess type of csv dialect = CSVSniffer().sniff(inFile.read(2048)) inFile.seek(0) rdr = CSVReader(inFile, dialect) itr = iter(rdr) headers = next(itr) rows = [] for r in itr: # check for empty row ( happens with some dat files originating from csvs) if len(''.join(r)) == 0: continue rows.append(r) return {'headers': headers, 'rows': rows}
def reader(file_name: str, encoding: Optional[str] = 'cp1252') \ -> Generator[List[str], None, None]: """Return iterator over rows in file `path_to_input_file` in CSV format. """ with open(file_name, mode='r', encoding=encoding) as csv_file: for row in CSVReader(csv_file, delimiter='\t', quotechar='"'): yield row
def read_bgm_name_task(): names = {} if os.path.exists(FILE_PATH): with open(FILE_PATH, 'r') as file: reader = CSVReader(file) for key, value in reader: names[key] = value return names
def read_channels_task(): names = [] if not os.path.exists(FILE_PATH): return names with open(FILE_PATH, 'r') as file: reader = CSVReader(file) for line in reader: describer = ChannelNameDescriber.from_csv_line(line) names.append(describer) return names
def ACLSetup(): ACL_List = [] with open('ACL.csv', newline='') as file: contents = CSVReader(file, delimiter=' ') for row in contents: ACL_List.append(row) ACL_List = ACL_List[1:] for i in range(len(ACL_List)): ACL_List[i] = ACL_List[i][0].split(',') return ACL_List
def parseLocations(inStream): reader = CSVReader(inStream) for (category, campName, textAddress, frontageAddress, crossStreet, dimensions) in reader: if category == b"Category" and campName == b"Camp Name": # Header row continue yield parseLocation( category=category, campName=campName, textAddress=textAddress, frontageAddress=frontageAddress, crossStreet=crossStreet, dimensions=dimensions, )
def _read_portfolio(self): self._symbols = [] self._currencies = [] self._weights = [] with open(self._csv_filepath, 'r') as csvfile: reader = CSVReader(csvfile, delimiter=',') is_header = True for row in reader: if len(row) != 3: continue if is_header: is_header = False continue # symbol,currency,weight logger.debug("Portofolio item: {}", row) self._symbols.append(row[0]) self._currencies.append(row[1]) self._weights.append(float(row[2]))
def read_csv_into_list_with_limit(csv_path, limit=10): a_list = list() with open(csv_path, newline='') as stream: for row in list(CSVReader(stream))[:limit]: a_list.append(row) return a_list
def read_csv_into_list(csv_path): a_list = list() with open(path.join("input", csv_path), newline='') as stream: for row in CSVReader(stream): a_list.append(row) return a_list
if __name__ == "__main__": if len(sys.argv) < 4: print('\nPlease provide date in the format: dd mm yyyy\n') sys.exit(1) # read input date date, month, year = sys.argv[1:] fulldate = f'{date}-{month}-{year}' csv_filename = CSV_FILENAME_FORMAT % (date, month, year[2:]) csv_filepath = os.path.join(DATA_DIR, 'csv', csv_filename) try: reader = CSVReader(open(csv_filepath, 'r'), delimiter=',') except FileNotFoundError: print('\nNo CSV file found for the given date\n') sys.exit(1) redis_instance = redis.Redis() with redis_instance.pipeline() as pipe: for row in reader: stock_name = row[1].strip() # ---- for each stock prepare a set with the dates it has entries ---- # # stock_name: {dd1-mm1-yyyy1, dd2-mm2-yyyy2}