Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
PlotGeo/add_classes.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
26 lines (22 sloc)
818 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import csv | |
import random | |
def rand_class(classes): | |
return classes[random.randint(0, len(classes)-1)] | |
def add_classes(csv_file_name, classes): | |
with open(csv_file_name) as csvfile: | |
with open('your_new_file.csv', 'w') as newcsvfile: | |
reader = csv.DictReader(csvfile) | |
writer = None | |
for row in reader: | |
if writer is None: | |
fieldnames = row.keys() | |
fieldnames.append('class') | |
writer = csv.DictWriter(newcsvfile, fieldnames=fieldnames) | |
writer.writeheader() | |
row['class'] = rand_class(classes) | |
writer.writerow(row) | |
if __name__ == '__main__': | |
csv_file_name = sys.argv[1] | |
classes = range(5) | |
add_classes(csv_file_name, classes) |