【问题标题】:Python script skip header row in excel filePython脚本跳过excel文件中的标题行
【发布时间】:2013-05-31 14:43:41
【问题描述】:

我编写了一个 python 脚本,它将从文件夹中提取 excel 文件并将它们写入 SQL 表。我让代码工作,但前提是我删除了包含标题的 excel 文件的第一行。我是 Python 新手,所以这可能很简单,但我研究了很多不同的技术,但不知道如何将它插入到我的代码中。任何想法将不胜感激!

# Import arcpy module
from xlrd import open_workbook ,cellname
import arcpy
import pyodbc as p

# Database Connection Info
server = "myServer"
database = "my_Tables"
connStr = ('DRIVER={SQL Server Native Client 10.0};SERVER=' + server + ';DATABASE=' + database + ';' + 'Trusted_Connection=yes')

# Assign path to Excel file
file_to_import = '\\\\Location\\Report_Test.xls'

# Assign column count
column_count=10

# Open entire workbook
book = open_workbook(file_to_import)

# Use first sheet
sheet = book.sheet_by_index(0)

# Open connection to SQL Server Table
conn = p.connect(connStr)

# Get cursor
cursor = conn.cursor()

# Assign the query string without values once, outside the loop
query = "INSERT INTO HED_EMPLOYEE_DATA (Company, Contact, Email, Name, Address, City, CentralCities, EnterpriseZones, NEZ, CDBG) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"

# Iterate through each row

for row_index in range(sheet.nrows):

    row_num          = row_index
    Company          = sheet.cell(row_index,0).value
    Contact          = sheet.cell(row_index,1).value
    Email            = sheet.cell(row_index,2).value
    Name             = sheet.cell(row_index,3).value
    Address          = sheet.cell(row_index,4).value
    City             = sheet.cell(row_index,5).value
    CentralCities    = sheet.cell(row_index,6).value
    EnterpriseZones  = sheet.cell(row_index,7).value
    NEZ              = sheet.cell(row_index,8).value
    CDBG             = sheet.cell(row_index,9).value

    values = (Company, Contact, Email, Name, Address, City, CentralCities, EnterpriseZones, NEZ, CDBG)

    cursor.execute(query, values)

# Close cursor
cursor.close()

# Commit transaction
conn.commit()

# Close SQL server connection
conn.close()

【问题讨论】:

  • 听起来您只需将for row_index in range(sheet.nrows): 更改为for row_index in range(1, sheet.nrows): 即可跳过第一行。
  • Excel 中没有任何内容显示“这是标题行”,因此您必须知道要跳过多少行(Aya 的评论)或猜测。

标签: python excel header skip


【解决方案1】:

您可以在第二行初始化迭代。请尝试以下操作:

for row_index in range(1,sheet.nrows):

编辑:如果您需要遍历 .xls 文件列表,正如您在 cmets 中询问的那样,基本思想是对文件执行外部循环。这里有一些提示:

# You need to import the os library. At the beinning of your code
import os

...
# Part of your code here
...

# Assign path to Excel file
#file_to_import = '\\\\Location\\Report_Test.xls'
folder_to_import = '\\\\Location'
l_files_to_import = os.listdir(folder_to_import)
for file_to_import in l_files_to_import:
    if file_to_import.endswith('.xls'):
        # The rest of your code here. Be careful with the indentation!
        column_count=10
        ...

【讨论】:

  • 哎呀,效果很好!谢谢!我认为这很明显,我只是想多了。
  • 如果我想更进一步并选择位于文件夹中的所有 excel 文件....会不会是 '*.xls' 之类的东西?我试过了,但它似乎不喜欢我的语法..
  • 您需要一个外部循环来遍历文件。您可以执行命令列出所有文件,然后对其进行迭代。这将是您已有的外部循环
  • 我会将外部循环放在脚本的哪个位置?我正在考虑将 file_to_import 更改为仅指向包含所有 XLS 文件的文件夹,然后立即启动外部循环。对不起,我对python很陌生。
  • 嘿米克尔,谢谢你的帮助!唯一阻止脚本运行的是我收到“IOError:[Errno 13] Permission denied”。我已将所有权限正确设置到它试图读取的 XLS 文件所在的文件夹,所以我不确定问题是什么。
【解决方案2】:

或者如果你很懒:

rows = 0
for row in ws.rows :
    if rows == 0 : 
        rows += 1
        continue
    ...

优点是您不必用复杂的单元格取消引用来愚弄:您拥有行对象并且可以执行row[col].value。更整洁。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 2017-02-22
    • 2018-09-10
    • 1970-01-01
    相关资源
    最近更新 更多