Reputation: 135
I am not so sure if this has been answered already or what not, and I am also not sure how to look it up so trying my luck here.
I am fairly new to Python and I am having this parameter to solve: to create a new file with the data extracted from the 2 .txt
files.
I have these following .txt
files:
# empList
# format: Employee number, First name, Last name, Department, Rate
201701013,Simona,Morasca,Finance,450;
201011003,Mitsue,Tollner,Marketing,750;
201409015,Leota,Dilliard,Finance,365;
199512017,Sage,Wieser,MIS,750;
199708003,Kris,Marrier,Admin,750;
# empMR
# format: Employee number, month, days worked
201612010,4,18;
201710017,4,12;
201701013,4,20;
201011003,4,24;
201409015,4,26;
199512017,4,28;
199708003,4,21;
What I am trying to achieve is:
I have this code I am experimenting with but alas, I am still unable to solve this.
while True:
with open("empList.txt", "r") as f1, open("empMR.txt", "r") as f2:
data1 = f1.read()
data2 = f2.read()
x1 = data1.strip().split(';')
x2 = data2.strip().split(',')
for (line1, line2) in (x1, x2):
line1 = line1.strip().split(',')
print(line2)
For the meantime, I am trying to print everything first so that I can see if my code is working since writing a file would take a lot of time and it's pretty the same with printing it.
Now here's the desired output:
I should be getting the: Employee number, Employee Name, Department, and the Rate from empList
; then from empMR
I should be getting the month, and the days worked. Print it out (or create a new file with those data) and that should keep me going.
I don't know if I made sense, but let me know if you need clarifications. Thanks!
Upvotes: 2
Views: 161
Reputation: 1
You can do it in this way:
with open('empList.txt', 'r') as w1, open("empMR.txt", "r") as w2:
list1 = [each.rstrip(';').split(',') for each in w1.read().splitlines()]
list2 = [each.rstrip(';').split(',') for each in w2.read().splitlines()]
new_data = [(e1+e2[1:]) for e1 in list1 for e2 in list2 if e1[0] == e2[0]]
for each in new_data:
print(each)
Output will be a list. But you can modify this easily:
['201701013', 'Simona', 'Morasca', 'Finance', '450', '4', '20']
['201011003', 'Mitsue', 'Tollner', 'Marketing', '750', '4', '24']
['201409015', 'Leota', 'Dilliard', 'Finance', '365', '4', '26']
['199512017', 'Sage', 'Wieser', 'MIS', '750', '4', '28']
['199708003', 'Kris', 'Marrier', 'Admin', '750', '4', '21']
Upvotes: 0
Reputation: 27567
Here is what you can do:
with open('empList.txt','r') as f1, open('empMR.txt','r') as f2:
l1, l2 = f1.readlines(), f2.readlines()
for s1 in l1:
a1 = s1.split(',')
for s2 in l2:
a2 = s2.split(',')
if a1[0] == a2[0]: # If the employee numbers match
num = a1[0]
fna = a1[1]
lna = a1[2]
dpt = a1[3]
rte = a1[4][:-2]
mth = a2[1]
wrk = a2[2][:-2]
print(num,fna,lna,dpt,rte,mth,wrk)
Output:
201701013 Simona Morasca Finance 450 4 20
201011003 Mitsue Tollner Marketing 750 4 24
201409015 Leota Dilliard Finance 365 4 26
199512017 Sage Wieser MIS 750 4 28
199708003 Kris Marrier Admin 75 4 2
Upvotes: 4