def test_csv_write_single_column(self): columns = ['name'] csv_content = ['cow', 'dog'] res = 'name\ncow\ndog\n' writeCSV(csv_content, file_name, columns) with open(file_name, 'r') as file: self.assertEqual(res, file.read())
def test_csv_write(self): csv_content = [['name', 'type'], ['cow', 'mammal']] res = 'name,type\ncow,mammal\n' writeCSV(csv_content, file_name) with open(file_name, 'r') as file: # append each line with a space self.assertEqual(res, file.read())
def test_csv_dict_write(self): csv_content = {'duck': 'bird', 'cow': 'mammal'} columns = ['animal', 'type'] res = 'animal,type\nduck,bird\ncow,mammal\n' writeCSV(csv_content, file_name, columns) with open(file_name, 'r') as file: # append each line with a space self.assertEqual(res, file.read())
def run(args): options = dealArgs(args).to_object() words = getAndFilter(options) stats = None # if stats being requested if options['stats']: stats = getStats(words) # format (incompatible with stats) elif options['format']: words = formatToLines(words) # if outputting, just print JSON for one if options['output']: # CSV if options['csv']: columns = ['Name'] if stats: columns = ['Name', 'Count'] writeCSV(stats, options['output'], columns) # Not CSV else: if stats: for_print = json.dumps(stats, indent=4) else: for_print = json.dumps(words, indent=4) writeFile(for_print, options['output']) print( f"\nThe requested data has been written to file {options['output']}." ) # if printing to screen else: for_print = '\n' if stats: if len(stats) <= 1: for_print += 'No stats to print.' else: for_print += 'STATS:\n' for stat in stats: for_print += f'\n{stat}: {stats[stat]}' else: if len(words) <= 1: for_print += 'No words to print.' else: for_print += 'WORDS:\n' for word in words: for_print += f'\n{word}' print(for_print)