Skip to content

Instantly share code, notes, and snippets.

@buhtz
Created November 25, 2020 10:04
Show Gist options
  • Save buhtz/4348522b65844293518f3a4b9d69fe0b to your computer and use it in GitHub Desktop.
Save buhtz/4348522b65844293518f3a4b9d69fe0b to your computer and use it in GitHub Desktop.
def write_data_to_csv(data):
"""
Write the previously generate data to a csv file.
"""
time_string = datetime.datetime.now().strftime('%Y%h%d_%H%M')
csv_name = f'{time_string}_for_spss-import.sim.csv'
with open(csv_name, 'w', encoding='utf-8', newline='') as csvfile:
# CSV writer instance
writer = csv.writer(csvfile, quoting=csv.QUOTE_NONNUMERIC,
delimiter=';',
quotechar='"')
# headline with column names
writer.writerow(col_names)
print(f'Number of fields in headline: {len(col_names)}')
print(f'Number of cases: {len(data)}')
#writer.writerows(data)
for d in data:
print(f'{len(d)}: {d}')
writer.writerow(d)
print(f'Data written to {csv_name} and simulation.csv.')
shutil.copy(csv_name, 'simulation.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment