python-记录文件、excel

1.收到了两个Excel文件,id值唯一,要求是找出两个文件的不同之处。

 

def get_sku(tablename, sheet, ids=0):
book = xlrd.open_workbook(tablename)
rows = book.sheet_by_name(sheet_name=sheet).nrows
for i in range(1, rows):
sku_List = []
for i in range(1, rows):
sku_List.append(book.sheet_by_name(sheet).row_values(i)[ids])
return sku_List

根据这个函数可以得到一个表,某个sheet 某列的一个列表,然后通过list.diff 来得到不同点。这时候我得到了一堆id,但是没有其他内容。。。如果直接将id发过去,
显然不够好。光看id 也看不出什么东西。。


2.那如果我知道了一个id 怎么知道这个id 在这个excel的具体位置呢,答案也是列表操作。将id那一列 用上面函数得到一个列表 然后用list.index(i) 来到这个id在这个列表的下标就可以
知道 这个id在表格中是多少行

number = tcl_sku.index(i)  # 返回a在指定切片内第一次出现的位置  得到了行数 


3.我现在知道id的行数,要得到一行全部内容。

class excel(object):
def __init__(self, file):
self.file = file
self.wb = load_workbook(self.file)
sheets = self.wb.get_sheet_names()
self.sheet = sheets[0]
self.ws = self.wb[self.sheet]

# 获取表格的总行数和总列数
def getRowsClosNum(self):
rows = self.ws.max_row
columns = self.ws.max_column
return rows, columns

# 获取某个单元格的值
def getCellValue(self, row, column):
cellvalue = self.ws.cell(row=row, column=column).value
return cellvalue

# 获取某列的所有值
def getColValues(self, column):
rows = self.ws.max_row
columndata = []
for i in range(1, rows + 1):
cellvalue = self.ws.cell(row=i, column=column).value
columndata.append(cellvalue)
return columndata

# 获取某行所有值
def getRowValues(self, row):
columns = self.ws.max_column
rowdata = []
for i in range(1, columns + 1):
cellvalue = self.ws.cell(row=row, column=i).value
rowdata.append(cellvalue)
return rowdata


根据这个 就可以得到某行某列的所有值。

这里i+2 i是指在这个id列表的下标,但是跟excel行数差距为2 ,所以i+2
data = excel('./test.xlsx').getRowValues(i + 2) 这里得到了一行所有内容。


4.写入到文件中。保存下来

mycology = open('./aab.txt', mode='a', encoding='utf-8')
for i in id[:2]: #这里我只从列表中取了两个做测试,实际过程中可以不需要[:2] 直接循环这个id 即可 id就是 在id列表中下标。
data = excel('./test.xlsx').getRowValues(i + 2)
print(str(data).replace("[", "").replace("]", ""), file=mycology)

python-记录文件、excel

 

 

这里它取到的数据 是一个列表,列表转化为字符串 ",".join(data) 也可以 我是用的 replace 去掉中括号



5. 根据title 写入到excel中
前面4 已经得到了一个文件。文件内容是差异的id 每一行的具体数据,现在根据excel title写入到excel中

python-记录文件、excel

 

 

def write_data_to_table(input_txt, out_excel):
"""
将txt文本写入到excel中
:return:
"""
titles = (open("./titles", "r").read()).split(",")
list3 = [] #空列表来放你的title 字段
for j in titles:
list3.append(j)
list2 = [list3]
data = (open(input_txt, "r", encoding="utf-8").read()).split("\n")
for i in data:
list1 = []
for j in i.split(","):
if j == "":
continue
else:
list1.append(j.replace("'", ""))
if not list1:
continue
try:
int(list1[0])
except:

list2.append(list1)
else:
list1.insert(0, "\t")
list2.append(list1)

# 打开文件
wb = openpyxl.load_workbook("./result.xlsx")

sheet = ["Sheet1", "Sheet2", "Sheet3"]
for i in sheet:
ws = wb[i]
wb.remove(ws)
ws = wb.create_sheet(title="Pip")
excel_path = out_excel
for i in list2:
ws.append(i)
if os.path.exists(excel_path):
os.remove(excel_path)
wb.save(excel_path)


if __name__ == '__main__':
write_data_to_table('./aab.txt', './res.xlsx')

python-记录文件、excel

 

 


解决~


当然还有更好的办法,只是我不知道而已。。




上一篇:嗯,怎么说呢?用Python来到处蹭蹭的实用案例,学会了都说好...


下一篇:python——pyftpdlib 快速搭建ftp服务器