简体   繁体   English

如何读取/打印特定的列和行python csv

[英]How to read/print specific column and rows python csv

I'm trying to read through a csv file (rows and columns like a spreadsheet) and and have it find specific point of data given 2 arguments 我正在尝试通读csv文件(行和列,如电子表格),并让它找到指定2个参数的数据点

def main(a, b): def main(a,b):

where a is the column and b is the row so that if I type in "A, 2" it would give me the name of the column (A) and the number in row 2. I'm not sure how to approach this. 其中a是列,b是行,因此如果我输入“ A,2”,它将为我提供列(A)的名称和第2行中的数字。我不确定该如何处理。

This is what I tried 这是我尝试过的

def data(a, b):

    file = open("file.csv")
    csv_file = csv.reader(file)

    for line in csv_file:
        array = line.split(",")
        first_item = array[0]

    a = len(array)
    csvfile.seek(0)

    reader = csv.reader(csv_file, delimiter=" ")

    for row in reader:
        b = list(row[a] for a in included_cols)
    print(content)

You can read the file into a 2D array and then use a, b to index into the array 您可以将文件读取到2D数组中,然后使用a,b索引到数组中

def data(a, b):
   array = []
   with open("file.csv") as file:
      for line in file.readlines():  
           array.append(line.split(","))
      print array[a][b]

Using with open("file.csv") as file, will close the file when you exit the with block of code 使用open(“ file.csv”)作为文件,当您退出with代码块时将关闭文件

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

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