使用 Python 和 Openpyxl 循环遍历 .xlsx,但循环只保存最后一行的数据
Using Python and Openpyxl to loop through a .xlsx, but loop only save the last row's data
我是一个 Python 新手,我正在做一个项目来自动化一个非常耗时的项目。我正在使用 openpyxl 访问 .xlsx 以提取信息,这些信息最终将转换为距离和方向方位角以与 arcpy/arcgis 一起使用,这意味着我正在使用 Python 2.7。我可以访问数据并进行第一轮更改,但我无法将写入命令集成到我的循环中。目前它将最后一行的数据保存到新 .xlsx 中给定范围内的所有单元格。这是我的代码:
#Importing OpenPyXl and loads the workbook and sheet
import openpyxl
wb = openpyxl.load_workbook('TESTVECT.xlsx')
ws = wb.get_sheet_by_name('TEST')
#allows to save more than once
write_only = False
cell_range = ws['C']
#sorts through either the rows/columns and slices the required string
maxRow = ws.max_row + 1
for row in range(2, maxRow):
parID = ws['A' + str(row)].value
Lline = ws['B' + str(row)].value
Vect = ws['C' + str(row)].value
print parID, Lline, Vect
trash, keep = Vect.split("C")
#This part save the very last row to all rows in available columns
#need a way to integrate the save functionality so each row is unique
for rowNum in range(2, maxRow):
ws.cell(row=rowNum, column=3).value = keep
for rowNum in range (2, maxRow):
ws.cell(row=rowNum, column=1).value = parID
for rowNum in range (2, maxRow):
ws.cell(row=rowNum, column=2).value = Lline
#Only prints the very last keep entry from the .xlsx
print keep
print "all done"
#Saving does not write all of the the 'keep, parID, and Lline' records
#There is an issue with the for loop and integrating the write portion of
#the code.
wb.save('TESTMONKEYVECT.xlsx')
谁能给我一些关于我在写入过程中做错了什么的指示,我需要每一行在进行更改后保留其唯一数据。
谢谢,
您的直觉是正确的,您需要合并循环。第一个循环遍历每一行,并在每个变量的最后一个值上保存 parID
、Lline
和 keep
。在循环之后,它们只有最后一行的值,因为这是唯一没有另一行出现在它之后并覆盖这些值的行。
您可以通过将操作合并为一个循环来解决此问题。
maxRow = ws.max_row + 1
for row in range(2, maxRow):
parID = ws['A' + str(row)].value
Lline = ws['B' + str(row)].value
Vect = ws['C' + str(row)].value
print parID, Lline, Vect
trash, keep = Vect.split("C")
ws.cell(row=rowNum, column=3).value = keep
ws.cell(row=rowNum, column=1).value = parID
ws.cell(row=rowNum, column=2).value = Lline
抱歉,我对这个话题很陌生 - 但也许这个例子有帮助:
#create loop to read from column of opened file and print from specific column row by row
i = 1
while i <= row_count:
ws_write.cell(row=i, column=col_write, value=sheet_read.cell(i, col_read).value)
i += 1
基本上您需要将值分配给循环内的单元格 - 正如前面的答案已经指出的那样
我是一个 Python 新手,我正在做一个项目来自动化一个非常耗时的项目。我正在使用 openpyxl 访问 .xlsx 以提取信息,这些信息最终将转换为距离和方向方位角以与 arcpy/arcgis 一起使用,这意味着我正在使用 Python 2.7。我可以访问数据并进行第一轮更改,但我无法将写入命令集成到我的循环中。目前它将最后一行的数据保存到新 .xlsx 中给定范围内的所有单元格。这是我的代码:
#Importing OpenPyXl and loads the workbook and sheet
import openpyxl
wb = openpyxl.load_workbook('TESTVECT.xlsx')
ws = wb.get_sheet_by_name('TEST')
#allows to save more than once
write_only = False
cell_range = ws['C']
#sorts through either the rows/columns and slices the required string
maxRow = ws.max_row + 1
for row in range(2, maxRow):
parID = ws['A' + str(row)].value
Lline = ws['B' + str(row)].value
Vect = ws['C' + str(row)].value
print parID, Lline, Vect
trash, keep = Vect.split("C")
#This part save the very last row to all rows in available columns
#need a way to integrate the save functionality so each row is unique
for rowNum in range(2, maxRow):
ws.cell(row=rowNum, column=3).value = keep
for rowNum in range (2, maxRow):
ws.cell(row=rowNum, column=1).value = parID
for rowNum in range (2, maxRow):
ws.cell(row=rowNum, column=2).value = Lline
#Only prints the very last keep entry from the .xlsx
print keep
print "all done"
#Saving does not write all of the the 'keep, parID, and Lline' records
#There is an issue with the for loop and integrating the write portion of
#the code.
wb.save('TESTMONKEYVECT.xlsx')
谁能给我一些关于我在写入过程中做错了什么的指示,我需要每一行在进行更改后保留其唯一数据。
谢谢,
您的直觉是正确的,您需要合并循环。第一个循环遍历每一行,并在每个变量的最后一个值上保存 parID
、Lline
和 keep
。在循环之后,它们只有最后一行的值,因为这是唯一没有另一行出现在它之后并覆盖这些值的行。
您可以通过将操作合并为一个循环来解决此问题。
maxRow = ws.max_row + 1
for row in range(2, maxRow):
parID = ws['A' + str(row)].value
Lline = ws['B' + str(row)].value
Vect = ws['C' + str(row)].value
print parID, Lline, Vect
trash, keep = Vect.split("C")
ws.cell(row=rowNum, column=3).value = keep
ws.cell(row=rowNum, column=1).value = parID
ws.cell(row=rowNum, column=2).value = Lline
抱歉,我对这个话题很陌生 - 但也许这个例子有帮助:
#create loop to read from column of opened file and print from specific column row by row
i = 1
while i <= row_count:
ws_write.cell(row=i, column=col_write, value=sheet_read.cell(i, col_read).value)
i += 1
基本上您需要将值分配给循环内的单元格 - 正如前面的答案已经指出的那样