In Python, I'm trying to export data as CSV for custom music tuning programs.
This is my goal (column1 goes from 0-127, column2 is a list of musical pitches expressed as frequencies). The Goal
I just need to remove this first row. Where I'm at
The conversation in this thread helped me lots. Writing data into CSV file
In playing with code from that thread, I reached my goal in Python Visualizer, but only for a portion of the code.
All my code:
# Ask user for the Home Frequency
print('What is your home frequency?')
raw_tonic = input()
t = int(raw_tonic)
# Ask user for an EDO
print('how many notes per octave do you want?')
raw_edo = input()
e = int(raw_edo)
# Spit out these frequencies into CSV
import csv
myFile = open("edotest9.csv", "w", newline="")
row = []
count = 0
writer = csv.writer(myFile)
row1 = []
for n in range(1, 129):
for i in range(1, 2):
row1.append(((t*2**((n-69)/e))))
count = count + 1
print(row1)
writer.writerow(row1)
row1[:] = [n]
myFile.close()
The general procedure for writing a CSV in python goes like this:
- Construct a 2D array. Each array in this 2D array represents your row in a CSV.
- Create CSV file by feeding this 2D array to the writerows method like I did below.
Code:
import csv
# Ask user for the Home Frequency
t = int(input('What is your home frequency?: '))
# Ask user for an EDO
e = int(input('how many notes per octave do you want?: '))
# Split out these frequencies into CSV
rows = []
column2_diff_pattern = [3, 3, 2, 3, 3, 3, 2]
diff_pattern_length = len(column2_diff_pattern)
column2 = 0
pattern_index = 0
for n in range(1, 129):
row = [n, t*2**((column2-69)/e)]
rows.append(row)
column2 = column2 + column2_diff_pattern[pattern_index]
pattern_index = (pattern_index + 1) % diff_pattern_length
# Do 'rows = rows[1:]' for removing the first row
with open('edotest9.csv', 'w') as csvFile:
writer = csv.writer(csvFile)
writer.writerows(rows)