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

时间:2017-09-20 21:49:03

标签: python excel

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

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

f.close()

3 个答案:

答案 0 :(得分:0)

这个问题对我来说并不完全清楚:我理解你想要从逗号分隔值文件中分离出来的值(顺便说一句,不是Excel文件)。您可以使用https://book.cakephp.org/2.0/en/core-utility-libraries/number.html将逗号分隔值拆分为列表:

2a5ef169.mp4?expires=1302948611&token=1290239327

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

答案 1 :(得分:0)

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

在这种情况下,Pythonic方式是使用标准库的Shortcode::compile('<h1>Users:</h1> [table table="users"]');模块,因为您似乎正在尝试读取.csv文件。所以,如果我理解正确,你需要以下几点:

csv

例如,给定输入文件:

Input File

......输出将是:

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

答案 2 :(得分:0)

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

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]
)

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