Answer to Question #244378 in Python for Hiruni

Question #244378
Develop a program to read the name, birthday and gender of a person from a file and output the name and ten-digit identity card (NIC) number to another file , each name starting on new line (see

The only input to the program is the name of the file. The output of the programme is the file "output.txt".
rules for NIC number.
The first four digits of the ID card is the birth year.
The next three digits are the number of days to the birth date from January 1st of that year. If the person is a female, 500 is added to that value.
The next three digits are assigned in the order of submission for a particular birth year.
The input file contains the records in the order of submission where each attribute is space separated.
In example , Aruni is the second entry in the year 1990. Therefore the last three digits of the NIC are 002.
Assume there are only 999 entries per year.
Example:
Saman 1990-05-03 M
Aruni 1990-04-06 F

Saman 1990123001




You may use the function,

days_to_birthday(date)
1
Expert's answer
2021-09-30T00:58:07-0400
from datetime import datetime
file_name = input()
file1 = open(file_name, 'r')
Lines = file1.readlines()
count = 0
year_prev = 0
NIC_Code = []
f = open("output.txt", "r+")
f.truncate(0)
for line in Lines:
  words = line.split()
  if year_prev==0:
    year_prev= words[1].split("-")[0]
  if year_prev != words[1].split("-")[0]:
    count = 0
  count += 1
  nic = ''
  nic=nic+words[0]
  start_date = str(words[1].split("-")[0])+"-01-01"
  days = ((datetime.strptime(words[1],"%Y-%m-%d") - datetime.strptime(start_date,"%Y-%m-%d")).days)+1
  if words[2]=="F":
    nic=nic+str(days+500)
  else:
    nic=nic+str(days)
  if count<10:
    nic=nic+"00"+str(count)
  elif count>=10 and count<100:
    nic=nic+"0"+str(count)
  else:
    nic=nic+str(count)
  print(nic)
  year_prev = words[1].split("-")[0]
  f.write(words[0]+" "+nic+"\n")
f.close()

Sample Input

task1.txt
Saman123001
Aruni596002

One file "output.txt" will be saved.




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS