Python验证文件标头及其元数据

时间:2019-01-27 02:18:23

标签: python pandas dataframe xlsx

我有一个文件名metas.txt,其中包含具有工作表名和col标题信息的所有xlsx文件的元数据 我需要进行一些验证,将metadata.txt与xlsx文件进行比较,并抛出异常。(下面提供了验证) 我有大约30张xlsx,带有不同的工作表(我提供了一些文件的示例) 我是python的新手,正在寻找有关如何实现它的建议/示例代码。

Validatons : 
Check metadata.txt and compare with emp.xlsx , dept.xlsx,locations.xlsx
(basically i need to loop filenames and sheetnames from metadata.txt with 
directory path C://Files) 
if there is mismatch in header(ie Col_header of metadata with header of 
xlsx(example: dept.xlsx(description not matching with dept_name) )
then throw error 
If there is duplicates found with column header 
(ex:locations.xlsx(loc_name repeated twice when it is compared with 
metadata.txt) throw error 

metadata.txt

filename:sheet_name:col_header
emp.xlsx:emp_details:emp_id,sal,dept_id,hiredate
dept.xlsx:dept_details:dept_id,dept_name,created_date
locations.xlsx:loc_details:loc_id,loc_name,created_date

emp.xlsx(sheetname:emp_details)

emp_id,sal,dept_id,hiredate 
1,2000,10,10-jan-2018
2,4000,20,12-jan-2018
3,5000,30,13-jan-2018

dept.xlsx(sheetname:dept_details)

dept_id,description,created_date
10,HR,10-apr-2018
20,IT,20-may-2018
30,MED,12-jun-2018

locations.xlsx(sheetname:loc_details)

loc_id,loc_name,created_date,loc_name
100,BAN,10-jan-17,BAN
200,CHE,20-jan-17,CHE

将我的结果打印到新文件中

File_name,count,systemdate,validationstatus
emp.xlsx,3,27-jan-19,succcess
dept.xlsx,3,27-jan-19,failed
locations.xlsx,3,27-jan-19,failed

1 个答案:

答案 0 :(得分:0)

有很多解决此问题的方法。以下是其中之一。从您的示例中,我看到选项卡名称并不重要,因此我没有在分析中包括它们。并且多余的空间也可能导致问题。我也在这里清洗它们。请享用。如果有任何不清楚的地方,请告诉我。希望对您有所帮助:

import json

with open('filename.txt', 'r') as file:
    result = json.loads('[' + file.read() + ']')