How about using pandas?
You can create a data set in DataFrame format first.
And then, you can export the DataFrame to a csv file.
Following is a simple example:
import pandas as pd
data = [['John', 25], ['Mary', 30], ['Bob', 20]] # Create a data
df = pd.DataFrame(data, columns=['Name', 'Age']) # Convert the data to DataFrame
df.to_csv('example.csv') # Export the DataFrame to csv file
The result of this code looks like this.