something like
import glob def parse_line(line): for item in ['Emp No', 'Name', 'SAL']: line = line.replace(item, '') return [item.strip() for item in line.split(':') if item.strip()] def process_file(file_name): with open(file_name) as f: # No need to specify 'r': this is the default. line = f.readlines()[6] return parse_line(line) pattern = 'C:/Test/Python/Mag/*.txt' files = glob.glob(pattern) for file_name in files: # 'file' is a builtin type, 'name' is a less-ambiguous variable name. emp_no, emp_name, emp_sal = process_file(file_name) # here add code to write to DBto test the parse_line function
def parse_line(line): for item in ['Emp No', 'Name', 'SAL']: line = line.replace(item, '') return [item.strip() for item in line.split(':') if item.strip()] my_file = """Please find below emp details, EMP DETAILS ________________________________ Emp No: 101 Name:RASUL K SAL: 30000 ________________________________ Join Date: 10 JAN 2010 From Native: First Releave Date: 25 APR 2018 To Native: Second Emp Designation: Manager No. of Projects handled: 10 """ line = my_file.split('\n')[6] print(parse_line(line))
Output:['101', 'RASUL K', '30000']
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs