如何以[Row1,Col1] [Row2,Col2] [Row3,Col3]格式读取和循环python中的excel文件

时间:2021-02-22 12:13:49

Up to the end of the file. I want to select the value of the resultant cell from each iteration. This what I have done so far:

直到文件的末尾。我想从每次迭代中选择结果单元格的值。这就是我到目前为止所做的:

with open('slatecode test.csv') as f:
    read_data = f.readlines()
    for row in read_data:
       print (row)

f.close()

3 个解决方案

#1


0  

The question is not completely clear to me: I understand you want to separate the values you get from your comma separated value file (not an Excel file by the way). You can use split to split comma separated values into a list:

这个问题对我来说并不完全清楚:我理解你想要从逗号分隔值文件中分离出来的值(顺便说一下,不是Excel文件)。您可以使用拆分将逗号分隔值拆分为列表:

with open('slatecode test.csv') as f:
   read_data = f.readlines()
   for row in read_data:
      list_of_row_values = row.split(",")
      print (list_of_row_values)

Note: f.close() is not needed: this is the purpose of the with block to handle file closing when necessary (end of block or error)

注意:不需要f.close():这是with块在必要时处理文件关闭的目的(块结束或错误)

#2


0  

Although the question is rather unclear, I will try to give you an answer anyway. It sounds like you only want to extract values going diagonally; first value being from ROW1, COL1, then value from ROW2, COL2, and so on.

虽然这个问题还不清楚,但无论如何我会试着给你一个答案。听起来你只想提取对角线的值;第一个值来自ROW1,COL1,然后是来自ROW2,COL2等的值。

In that case, the Pythonic way would be to use standard library's csv module, since it seems that you are trying to read a .csv file. So, if I understood you correctly, you want something along the following lines:

在这种情况下,Pythonic方式将使用标准库的csv模块,因为您似乎正在尝试读取.csv文件。所以,如果我理解正确,你需要以下几点:

import csv

with open(file_name, 'r', newline='') as f:

    reader = csv.reader(f, delimiter=',')

    for column, row in enumerate(reader):
        try:
            print(row[column])
        except IndexError: # in case there are more columns than rows
            break

For instance, given the input file:

例如,给定输入文件:

如何以[Row1,Col1] [Row2,Col2] [Row3,Col3]格式读取和循环python中的excel文件

...the output would be:

...输出将是:

1
7
13
19
25

#3


0  

I was able to answer it myself and here is my code:

我能够自己回答,这是我的代码:

import csv

with open('slatecode test.csv') as f:
    readCSV = csv.reader(f)
    col1 = []
    col2 = []
    col3 = []
    col4 = []
    col5 = []
    col6 = []
    col7 = []
    col8 = []
    col9 = []
    col10 = []
    for row in readCSV:
        #print (row)
        col1.append(row[0])
        col2.append(row[1])
        col3.append(row[2])
        col4.append(row[3])
        col5.append(row[4])
        col6.append(row[5])
        col7.append(row[6])
        col8.append(row[7])
        col9.append(row[8])
        col10.append(row[9])

    row1 = [col1[0], col2[0], col3[0], col4[0], col5[0], col6[0], col7[0], col8[0], col9[0], col10[0]]
    row2 = [col1[1], col2[1], col3[1], col4[1], col5[1], col6[1], col7[1], col8[1], col9[1], col10[1]]
    row3 = [col1[2], col2[2], col3[2], col4[2], col5[2], col6[2], col7[2], col8[2], col9[2], col10[2]]
    row4 = [col1[3], col2[3], col3[3], col4[3], col5[3], col6[3], col7[3], col8[3], col9[3], col10[3]]
    row5 = [col1[4], col2[4], col3[4], col4[4], col5[4], col6[4], col7[4], col8[4], col9[4], col10[4]]
    row6 = [col1[5], col2[5], col3[5], col4[5], col5[5], col6[5], col7[5], col8[5], col9[5], col10[5]]
    row7 = [col1[6], col2[6], col3[6], col4[6], col5[6], col6[6], col7[6], col8[6], col9[6], col10[6]]
    row8 = [col1[7], col2[7], col3[7], col4[7], col5[7], col6[7], col7[7], col8[7], col9[7], col10[7]]
    row9 = [col1[8], col2[8], col3[8], col4[8], col5[8], col6[8], col7[8], col8[8], col9[8], col10[8]]
    row10 = [col1[9], col2[9], col3[9], col4[9], col5[9], col6[9], col7[9], col8[9], col9[9], col10[9]]

    print([row1, col1])
    print([row2, col2])
    print([row3, col3])
    print([row4, col4])
    print([row5, col5])
    print([row6, col6])
    print([row7, col7])
    print([row8, col8])
    print([row9, col9])
    print([row10, col10])

    print(col1[0], col2[0], col3[0], col4[0], col5[0], col6[0], col7[0], col8[0], col9[0], col10[0],
col1[1], col2[1], col3[1], col4[1], col5[1], col6[1], col7[1], col8[1], col9[1], col10[1],
col1[2], col2[2], col3[2], col4[2], col5[2], col6[2], col7[2], col8[2], col9[2], col10[2],
col1[3], col2[3], col3[3], col4[3], col5[3], col6[3], col7[3], col8[3], col9[3], col10[3],
col1[4], col2[4], col3[4], col4[4], col5[4], col6[4], col7[4], col8[4], col9[4], col10[4],
col1[5], col2[5], col3[5], col4[5], col5[5], col6[5], col7[5], col8[5], col9[5], col10[5],
col1[6], col2[6], col3[6], col4[6], col5[6], col6[6], col7[6], col8[6], col9[6], col10[6],
col1[7], col2[7], col3[7], col4[7], col5[7], col6[7], col7[7], col8[7], col9[7], col10[7],
col1[8], col2[8], col3[8], col4[8], col5[8], col6[8], col7[8], col8[8], col9[8], col10[8],
col1[9], col2[9], col3[9], col4[9], col5[9], col6[9], col7[9], col8[9], col9[9], col10[9]
)

The code looks too long. I want to know if there is a better, shorter and more efficient way of writing this code.

代码看起来太长了。我想知道是否有更好,更短,更有效的编写代码的方法。

#1


0  

The question is not completely clear to me: I understand you want to separate the values you get from your comma separated value file (not an Excel file by the way). You can use split to split comma separated values into a list:

这个问题对我来说并不完全清楚:我理解你想要从逗号分隔值文件中分离出来的值(顺便说一下,不是Excel文件)。您可以使用拆分将逗号分隔值拆分为列表:

with open('slatecode test.csv') as f:
   read_data = f.readlines()
   for row in read_data:
      list_of_row_values = row.split(",")
      print (list_of_row_values)

Note: f.close() is not needed: this is the purpose of the with block to handle file closing when necessary (end of block or error)

注意:不需要f.close():这是with块在必要时处理文件关闭的目的(块结束或错误)

#2


0  

Although the question is rather unclear, I will try to give you an answer anyway. It sounds like you only want to extract values going diagonally; first value being from ROW1, COL1, then value from ROW2, COL2, and so on.

虽然这个问题还不清楚,但无论如何我会试着给你一个答案。听起来你只想提取对角线的值;第一个值来自ROW1,COL1,然后是来自ROW2,COL2等的值。

In that case, the Pythonic way would be to use standard library's csv module, since it seems that you are trying to read a .csv file. So, if I understood you correctly, you want something along the following lines:

在这种情况下,Pythonic方式将使用标准库的csv模块,因为您似乎正在尝试读取.csv文件。所以,如果我理解正确,你需要以下几点:

import csv

with open(file_name, 'r', newline='') as f:

    reader = csv.reader(f, delimiter=',')

    for column, row in enumerate(reader):
        try:
            print(row[column])
        except IndexError: # in case there are more columns than rows
            break

For instance, given the input file:

例如,给定输入文件:

如何以[Row1,Col1] [Row2,Col2] [Row3,Col3]格式读取和循环python中的excel文件

...the output would be:

...输出将是:

1
7
13
19
25

#3


0  

I was able to answer it myself and here is my code:

我能够自己回答,这是我的代码:

import csv

with open('slatecode test.csv') as f:
    readCSV = csv.reader(f)
    col1 = []
    col2 = []
    col3 = []
    col4 = []
    col5 = []
    col6 = []
    col7 = []
    col8 = []
    col9 = []
    col10 = []
    for row in readCSV:
        #print (row)
        col1.append(row[0])
        col2.append(row[1])
        col3.append(row[2])
        col4.append(row[3])
        col5.append(row[4])
        col6.append(row[5])
        col7.append(row[6])
        col8.append(row[7])
        col9.append(row[8])
        col10.append(row[9])

    row1 = [col1[0], col2[0], col3[0], col4[0], col5[0], col6[0], col7[0], col8[0], col9[0], col10[0]]
    row2 = [col1[1], col2[1], col3[1], col4[1], col5[1], col6[1], col7[1], col8[1], col9[1], col10[1]]
    row3 = [col1[2], col2[2], col3[2], col4[2], col5[2], col6[2], col7[2], col8[2], col9[2], col10[2]]
    row4 = [col1[3], col2[3], col3[3], col4[3], col5[3], col6[3], col7[3], col8[3], col9[3], col10[3]]
    row5 = [col1[4], col2[4], col3[4], col4[4], col5[4], col6[4], col7[4], col8[4], col9[4], col10[4]]
    row6 = [col1[5], col2[5], col3[5], col4[5], col5[5], col6[5], col7[5], col8[5], col9[5], col10[5]]
    row7 = [col1[6], col2[6], col3[6], col4[6], col5[6], col6[6], col7[6], col8[6], col9[6], col10[6]]
    row8 = [col1[7], col2[7], col3[7], col4[7], col5[7], col6[7], col7[7], col8[7], col9[7], col10[7]]
    row9 = [col1[8], col2[8], col3[8], col4[8], col5[8], col6[8], col7[8], col8[8], col9[8], col10[8]]
    row10 = [col1[9], col2[9], col3[9], col4[9], col5[9], col6[9], col7[9], col8[9], col9[9], col10[9]]

    print([row1, col1])
    print([row2, col2])
    print([row3, col3])
    print([row4, col4])
    print([row5, col5])
    print([row6, col6])
    print([row7, col7])
    print([row8, col8])
    print([row9, col9])
    print([row10, col10])

    print(col1[0], col2[0], col3[0], col4[0], col5[0], col6[0], col7[0], col8[0], col9[0], col10[0],
col1[1], col2[1], col3[1], col4[1], col5[1], col6[1], col7[1], col8[1], col9[1], col10[1],
col1[2], col2[2], col3[2], col4[2], col5[2], col6[2], col7[2], col8[2], col9[2], col10[2],
col1[3], col2[3], col3[3], col4[3], col5[3], col6[3], col7[3], col8[3], col9[3], col10[3],
col1[4], col2[4], col3[4], col4[4], col5[4], col6[4], col7[4], col8[4], col9[4], col10[4],
col1[5], col2[5], col3[5], col4[5], col5[5], col6[5], col7[5], col8[5], col9[5], col10[5],
col1[6], col2[6], col3[6], col4[6], col5[6], col6[6], col7[6], col8[6], col9[6], col10[6],
col1[7], col2[7], col3[7], col4[7], col5[7], col6[7], col7[7], col8[7], col9[7], col10[7],
col1[8], col2[8], col3[8], col4[8], col5[8], col6[8], col7[8], col8[8], col9[8], col10[8],
col1[9], col2[9], col3[9], col4[9], col5[9], col6[9], col7[9], col8[9], col9[9], col10[9]
)

The code looks too long. I want to know if there is a better, shorter and more efficient way of writing this code.

代码看起来太长了。我想知道是否有更好,更短,更有效的编写代码的方法。