My First Python Project at my company

 Description: There were 3 text files of attendance
After merging three file I made  a report. 
Porblem was some all employess are not present/exists in all text files
Also I read some information from my legacy foxplus master.dbf
code is as under
-------------------------------------------------------------------------------------
 def fetchline(infile,mdays):
    with open(infile, "r", encoding="utf8") as txtf:
        retline=[]
        for line in txtf:
            if empcode in line:
                retline = line.split("\t")
                return retline[2:] ## leave the code and name fields
    txtf.close()
    if len(retline) < 3:
        retline=['A' for i in range(mdays)]
    return retline

def emplist(infile):   
    with open(infile, "r", encoding="utf8") as txtf:
        retlist=[]
        for line in txtf:
            r=line.split()            
            print(r[0],file=codes)
            #if len(r)> 4 and r[0].isnumeric:
            retlist.append(r[0])
    txtf.close()
    return retlist

def dates(infile):
    with open(infile, "r", encoding="utf8") as txtf:
        retdates=txtf.readline().split()# first line is heading line dates are started from [2:] to end
        #retdates=retdates[(retdates.index('NAME'))+1:] # first 2 columns are code,name leave them take only dates col
        retdates=retdates[3:] # first 2 columns are code,name leave them take only dates col
        retdates=[item for item in retdates if item.strip() !='' ] # it removes all '' items and '\n' items
    txtf.close()
    return retdates

# 
print('''
        1. Copy master.dbf in current dir 
        3. The program will fetch Recors from master.dbf newno, join date, left date
        2. Out file will be delimited with tab ''' )
import datetime
mydate  = datetime.datetime.now()

# Make default file like aug-23.txt
infile1  = 'jun-23.txt'
infile2  = 'jul-23.txt'
infile3  = mydate.strftime("%b").lower()+'-'+mydate.strftime("%y")+'.txt'
outfile = mydate.strftime("%b").lower()+'-'+mydate.strftime("%y")+'.csv'
infile3  = input('Enter 3. Input  text File default is ['+infile3 +']:') or infile3
outfile = input('Enter Output xls  File default is ['+outfile+']:') or outfile

print('Please Wait for 3 minutes')
codes=open('codes.txt','w') 
empcodes=emplist(infile1) +emplist(infile3)+emplist(infile3)
# stop
month1=dates(infile1)
month2=dates(infile2)
month3=dates(infile3)

mdays1,mdays2,mdays3 = len(month1),len(month2),len(month3)
dates=month1+month2+month3

# to make every emp code UNIQUE to avoid repeated codes
empcodes=set(empcodes)
empcodes=list(empcodes) 

from dbfread import DBF
output = open(outfile,'w')
print('CODE','NAME','JOINED','LEFT ON','ABSENT DAYS','LAST DUTY',sep='\t',file=output)
##with open(infile3, "r", encoding="utf8") as txtf:
dates.reverse()
for empcode in empcodes:
    if empcode.isalpha():
        continue
    #print(empcode)
    #print(d)
    ## r is rec in list format 
    r=fetchline(infile1,mdays1)+fetchline(infile2,mdays2)+fetchline(infile3,mdays3)
    # Note that due to reverse NAME goes to last
    # find first occurance of "P" in reverse order that is last duty
    r=[i for i in r if not ( i == "\n" or i == '')]
    r.reverse()
    #print(r)
    #input('check value of r')
    if 'P' not in r:
        print(r)
    index=r.index('P') # out put will number
    absents = r[0:index].count('A')## consecutive absents after last present
    #r.reverse()
    #print(r)
    joindate='        '
    left    ='        '
    name    = ' '.ljust(35)
    for mastrec in DBF('master.dbf'):
            #print(mastrec)
        if mastrec['NEWNO'] == empcode:
            name    =mastrec['NAME']
            joindate=mastrec['JOINDATE']
            left    =mastrec['LEFT']
            break
    if left == None :
        left    ='        '
    print('"'+empcode+'"',name,joindate,left,absents,dates[index],sep=',',file=output)
    #print('"'+empcode+'"',name,joindate,left,absents,dates[index],sep='\t')

    #print(r[0],r[1],joindate,left,r[2:].count('A',index),d[index],sep='\t',file=output)
    #input('check final out put')
output.close()
print('The Output file',outfile,' was created open it in Excel,')
print('Thanks')
print('Task Completed Successfully')


Comments

Popular posts from this blog

Course No 2 Using Python to Interact with the Operating System Rough Notes

HOW TO WRITE AND SELL EBOOKS USING AI SUPERCOOL.COM

WEB SCRAPING WITH PYTHON: https://campus.datacamp.com/courses/web-scraping-with-python