Writing array to csv python (one column)

前端 未结 5 922
一整个雨季
一整个雨季 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 02:45

    You need to write each item of list to a row in the CSV file to get them into one column.

    for label in testLabels:
        wr.writerows([label])
    
    0 讨论(0)
  • 2021-01-14 02:58

    You should change the delimiter. CSV is Comma Separated Value, but Excel understands that a comma is ";" (yeah weird). So you have to add the option delimiter=";", like

    csv.writer(myfile, delimiter=";")
    
    0 讨论(0)
  • 2021-01-14 02:59

    Try this:

    wtr = csv.writer(open ('out.csv', 'w'), delimiter=',', lineterminator='\n')
    for x in arr : wtr.writerow ([x])
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2021-01-14 03:09

    Try this:

        for i in range(len(testLabels)):
            result_file = open('filePath.csv', 'a')
            result_file.write("{}{}".format(testLabels[i], '\n'))
    
    0 讨论(0)
提交回复
热议问题