简体   繁体   中英

Python csv list of tuples to columns

I Have a list of (x,y) tuples, being both x and y lists of their own, like such:

[
([44.651162790697676, 44.651162790697676, 44.651162790697676, 44.651162790697676, 44.651162790697676, 44.651162790697676, 44.651162790697676, 44.651162790697676, 44.651162790697676, 44.651162790697676]
, [42.592592592592595, 42.592592592592595, 42.592592592592595, 42.592592592592595, 42.592592592592595, 42.592592592592595, 42.592592592592595, 42.592592592592595, 42.592592592592595, 42.592592592592595])
]

The main idea is to have more than one tuple in this list, just used one for example purposes.

I need to put each list from the tuple into a spreadsheet column, and to do that I'm using the following code:

def saveSpreadsheet(fpath, fname, list):
index = 1
for sublist in list:
    with open(os.path.join(fpath, fname + str(index) + '.csv'), 'w', newline='') as myfile:
        writer = csv.writer(myfile, delimiter=';')
        writer.writerow(("training","test"))
        for row in sublist:
            writer.writerow(row)
        index += 1

The method takes a folder path, file name and the said list, and produces a csv. My issue with it is that is it not saving in columns but in rows:

在此处输入图片说明

The supposed output should look like this:

在此处输入图片说明

What am i doing wrong here?

Given your data:

data = [
     (
        [44.651162790697676, 44.651162790697676, 44.651162790697676, 44.651162790697676, 44.651162790697676, 44.651162790697676, 44.651162790697676, 44.651162790697676, 44.651162790697676, 44.651162790697676],
        [42.592592592592595, 42.592592592592595, 42.592592592592595, 42.592592592592595, 42.592592592592595, 42.592592592592595, 42.592592592592595, 42.592592592592595, 42.592592592592595, 42.592592592592595]
     )
]

We need to convert it to something you can write row by row. Zip works well:

for a in zip(data[0][0], data[0][1]):
    print(a)

Which result in:

(44.651162790697676, 42.592592592592595)
(44.651162790697676, 42.592592592592595)
(44.651162790697676, 42.592592592592595)
(44.651162790697676, 42.592592592592595)
(44.651162790697676, 42.592592592592595)
(44.651162790697676, 42.592592592592595)
(44.651162790697676, 42.592592592592595)
(44.651162790697676, 42.592592592592595)
(44.651162790697676, 42.592592592592595)
(44.651162790697676, 42.592592592592595)

I think you have a good handle on the rest and can adapt this. See this similar (duplicate?) question for more details: Python: CSV write by column rather than row

The suggestions in the comments both offer good advice. If the length of the columns isn't guaranteed to be equal, itertools.zip_longest() is the way to go. Pandas does offer more features and better effiency. Don't optimize prematurely though. If this does all you need and you aren't running into issues, you can get away with this. Pandas does take some time to learn but in the long run it probably is worth it if you'll be doing more of this stuff.

Although Zev provided great help, I ended up following Massoud Hosseinali advice and went with pandas, which was a lot more efficient and concise.

Instead of having a list of tuples with lists like i had before: [([],[])] , if I save them data as a list of lists containing tuples: [[(,)]] , i can simply iterate through each sublist, make the tuple into a pd.Series , convert it to a pd.DataFrame and save is as a .csv using pd.to_csv .

Here is the resulting code:

def saveSpreadsheet(fpath, fname, list):
index = 1
labels = ['training', 'test']
for sublist in list:
    training = pd.Series(sublist[0])
    test = pd.Series(sublist[1])
    df = pd.DataFrame({'training':training.values, 'test':test.values})
    df.to_csv(os.path.join(fpath, fname + str(index) + '.csv'), sep=";",  index=False)
    index += 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM