Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python循环excel表格打印单元格值_Python_Excel - Fatal编程技术网

Python循环excel表格打印单元格值

Python循环excel表格打印单元格值,python,excel,Python,Excel,我试图在excel表格中循环打印每列中的值。然后转到下一行,打印列的值,直到文件结束 我的数据看起来像 Name email desc date name1 email1 desc1 date1 name2 email2 desc2 date2 name3 email3

我试图在excel表格中循环打印每列中的值。然后转到下一行,打印列的值,直到文件结束

我的数据看起来像

Name           email                desc           date
name1          email1               desc1          date1
name2          email2               desc2          date2
name3          email3               desc3          date3



wb = load_workbook('example.xlsx')
sheet = wb.get_sheet_by_name('Sheet1')
row_count = sheet.max_row -1
column_count = sheet.max_column
i = 1
while (i <= row_count):
 for item in sheet.iter_cols():
    #for row in sheet.iter_cols():
    first_value = item[i]
    print (first_value.value)
    i+=1
在我得到超出范围的错误之前使用上面的代码
name1、email2、desc3作为输出,这显然是错误的,因为数据不在同一行中

事实上,您正在打印像
name1
email2
desc3
这样的值,这表明您在列和行上都在递增。这是因为您正在增加嵌套for循环中的
i
,而不是在外部循环中

尝试在while循环中增加它。例如,类似这样的事情:

while (i <= row_count):
 for item in sheet.iter_cols():
   #for row in sheet.iter_cols():
   first_value = item[i]
   print (first_value.value)
 i+=1

while(i解决此问题的最简单方法是将文件另存为.csv

然后运行:

import csv
with open(<YOUR_CSV_FILE_NAME>) as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        print(row)
导入csv
使用open()作为csvfile:
reader=csv.DictReader(csvfile)
对于读取器中的行:
打印(行)
从这里,您可以随意使用“row”变量

的可能重复项。请给出完整的代码和完整的错误消息。
import csv
with open(<YOUR_CSV_FILE_NAME>) as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        print(row)