Writing array to csv python (one column)

前端 未结 5 956
一整个雨季
一整个雨季 2021-01-14 02:09

I\'m trying to write the values of an array to a .csv file in python. But when I open the file in excel, the data is shown in one row. I want to have one column where each m

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-14 03:01

    Try this:

    import csv
    import numpy as np
    yourArray = ['deer', 'airplane', 'dog', ..., 'frog', 'cat', 'truck']
    yourArray = np.array(yourArray)
    
    with open('outputFile.csv', 'w', newline='') as csvfile:
        writer = csv.writer(csvfile, delimiter=',')
        for row in range(0,yourArray.shape[0]):
            myList = []
            myList.append(yourArray[row])
            writer.writerow(myList)
    

提交回复
热议问题