简体   繁体   English

Python CSV元组到列的列表

[英]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: 我有一个(x,y)元组的列表,同时是它们自己的x和y列表,例如:

[
([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. 该方法采用文件夹路径,文件名和上述列表,并生成一个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 有关更多详细信息,请参见类似的问题(重复的问题): Python:按列而不是按行写CSV

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. 如果不能保证列的长度相等,则可以使用itertools.zip_longest() 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. 尽管Zev提供了很大的帮助,但最终我还是遵循了Massoud Hosseinali的建议,并选择了熊猫,这更加高效和简洁。

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 . 不用像以前那样具有元组列表: [([],[])] ,如果我将它们的数据保存为包含元组的列表列表: [[(,)]] ,我可以简单地遍历每个子列表,将元组转换为pd.Series ,将其转换为pd.DataFrame然后使用pd.to_csv保存为.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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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