gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
Write me some python code that writes a 1d array to a csv file
- User
Write me some python code that writes a 1d array to a csv file
- Assistant
Sure! Here's an example of Python code that writes a 1D array to a CSV file:
pythonimport csv def write_array_to_csv(array, filename): with open(filename, 'w', newline='') as csvfile: writer = csv.writer(csvfile) writer.writerow(array) # Example usage my_array = [1, 2, 3, 4, 5] write_array_to_csv(my_array, 'output.csv')In this code, the
write_array_to_csv()function takes in two parameters:array(the 1D array to be written) andfilename(the name of the CSV file to be created).The function uses the
csv.writer()class to create a writer object. It then callswriter.writerow(array)to write the array as a single row in the CSV file.Finally, an example usage is shown where
my_arrayis defined andwrite_array_to_csv()is called with the array and the desired output filename.