Python: CSV write by column rather than row

后端 未结 7 952
春和景丽
春和景丽 2020-12-01 03:46

I have a python script that generates a bunch of data in a while loop. I need to write this data to a CSV file, so it writes by column rather than row.

For example in

相关标签:
7条回答
  • 2020-12-01 04:07
    wr.writerow(item)  #column by column
    wr.writerows(item) #row by row
    

    This is quite simple if your goal is just to write the output column by column.

    If your item is a list:

    yourList = []
    
    with open('yourNewFileName.csv', 'w', ) as myfile:
        wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
        for word in yourList:
            wr.writerow([word])
    
    0 讨论(0)
  • 2020-12-01 04:10

    Updating lines in place in a file is not supported on most file system (a line in a file is just some data that ends with newline, the next line start just after that).

    As I see it you have two options:

    1. Have your data generating loops be generators, this way they won't consume a lot of memory - you'll get data for each row "just in time"
    2. Use a database (sqlite?) and update the rows there. When you're done - export to CSV

    Small example for the first method:

    from itertools import islice, izip, count
    print list(islice(izip(count(1), count(2), count(3)), 10))
    

    This will print

    [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9), (8, 9, 10), (9, 10, 11), (10, 11, 12)]
    

    even though count generate an infinite sequence of numbers

    0 讨论(0)
  • 2020-12-01 04:11

    As an alternate streaming approach:

    • dump each col into a file
    • use python or unix paste command to rejoin on tab, csv, whatever.

    Both steps should handle steaming just fine.

    Pitfalls:

    • if you have 1000s of columns, you might run into the unix file handle limit!
    0 讨论(0)
  • 2020-12-01 04:15

    what about Result_* there also are generated in the loop (because i don't think it's possible to add to the csv file)

    i will go like this ; generate all the data at one rotate the matrix write in the file:

    A = []
    
    A.append(range(1, 5))  # an Example of you first loop
    
    A.append(range(5, 9))  # an Example of you second loop
    
    data_to_write = zip(*A)
    
    # then you can write now row by row
    
    0 讨论(0)
  • 2020-12-01 04:17

    The reason csv doesn't support that is because variable-length lines are not really supported on most filesystems. What you should do instead is collect all the data in lists, then call zip() on them to transpose them after.

    >>> l = [('Result_1', 'Result_2', 'Result_3', 'Result_4'), (1, 2, 3, 4), (5, 6, 7, 8)]
    >>> zip(*l)
    [('Result_1', 1, 5), ('Result_2', 2, 6), ('Result_3', 3, 7), ('Result_4', 4, 8)]
    
    0 讨论(0)
  • 2020-12-01 04:17

    Read it in by row and then transpose it in the command line. If you're using Unix, install csvtool and follow the directions in: https://unix.stackexchange.com/a/314482/186237

    0 讨论(0)
提交回复
热议问题