简体   繁体   English

如何将CSV文件中的行中的值附加到python中的列表中?

[英]How do I append values from a row in a CSV file into a list in python?

If I have a csv file with only two rows containing an x and y coordinate, how would I go about taking row[0] and all its x points and appending them to a list? 如果我有一个只有两行包含x和y坐标的csv文件,我将如何获取row [0]及其所有x点并将它们附加到列表中?

In other words, lets say I have a list: 换句话说,假设我有一个列表:

[[1,2], [3,5]]

Is there a way to take 1 and 3 and put them into a new list, and then 2 and 5 into another list? 是否可以将13放入新列表,然后将25放入另一个列表?

points = [[1,2], [3,5]] #[[x,y],[x,y]]
x,y = zip(*points) #x = [1,3],y=[2,5]

this is an easy way to transpose any 2d array (take the rows and change them to columns) 这是转置任何二维数组的简单方法(获取行并将其更改为列)

if you had 3d points for example 例如,如果您有3d点

points = [[1,2,3],[2,3,4],[4,5,6]]
x,y,z = zip(*points)

you can get your points from the csv 您可以从csv中获取您的points

points = list(csv.reader(open("my_file.csv")))

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

相关问题 如何将 append 列为 csv 文件中的行 python? - How to append a list as row in csv file in python? Python 3如何在文件中附加列表? - Python 3 How do I append a list in a file? 如何将 csv 文件中的值添加到列表中? - How do I add values from a csv file to a list? Append 值从 csv 文件到列表然后排序 - Append values from csv file to a list and then sort 如何循环并将我的代码附加到 CSV 文件? - Python - How do I loop and append my code to a CSV file? - Python 如何从带有图像注释的 CSV 文件中提取值并将它们附加到 Python 中 RetinaNet 的新 CSV 文件中? - How can I extract values from a CSV file with image annotations and append them to a new CSV file for RetinaNet in Python? Python附加列标题并将列值从列表附加到csv - Python append column header & append column values from list to csv 使用python在csv中每一行的末尾附加字典或值列表 - Append Dictionary or list of values at the end of each row in csv using python 如何将字典列表中的值附加到新列表中? - How do I append values from a list of dictionaries into a new list? Python > Selenium + CSV:如何从 .csv 文件中的列表打开链接、循环代码、在 csv 上附加数据? - Python > Selenium + CSV: How to open links from list in .csv file, loop code, append data on csv?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM