gpt4 book ai didi

python - 使用python在excel中删除具有某些列组合的重复行

转载 作者:太空宇宙 更新时间:2023-11-04 07:33:11 24 4
gpt4 key购买 nike

我有一个读取 excel 文档的 python 程序。我只需要允许某些列组合的首次出现。例如:

    A     |  B
-------------
1. 200 | 201
2. 200 | 202
3. 200 | 201
4. 200 | 203
5. 201 | 201
6. 201 | 202
.............

我想删除/跳过发现重复项的第三行并将其写入 CSV 文件。这是我到目前为止一直在尝试的功能。但它不起作用。

def validateExcel(filename):
xls=xlrd.open_workbook(filename)
setcount = 0
column = 0
count = 0
# sheetcount = 0
for sheet in xls.sheets():
header=""
# sheetcount = sheetcount + 1
number_of_rows = sheet.nrows
number_of_columns = sheet.ncols
sheetname = sheet.name
mylist = []
for row in range (1, number_of_rows):
mylist = []
for col in range(0, 2):
mylist.append(sheet.cell_value(row, col))

print mylist

myset = set(mylist)

print myset

最佳答案

它对我有用:在 python 2.7 中

def validateExcel(filename):
xls=xlrd.open_workbook(filename)
setcount = 0
column = 0
count = 0
# sheetcount = 0
for sheet in xls.sheets():
header=""
# sheetcount = sheetcount + 1
number_of_rows = sheet.nrows
number_of_columns = sheet.ncols
sheetname = sheet.name
mylist = []
for row in range(1, number_of_rows):
mylist.append((sheet.cell_value(row, 0), sheet.cell_value(row, 1)))
myset = sorted(set(mylist), key=mylist.index)
return myset

关于python - 使用python在excel中删除具有某些列组合的重复行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42759094/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com