A
A
Anastasia Bodrova2021-03-20 21:05:42
css
Anastasia Bodrova, 2021-03-20 21:05:42

How to use the value of the variable to get to the name of the employee in another file?

Hello, I want to display the name of the employee who has the highest salary for the first half of the year, but so far I can only display his TIN, since there are two files:
1) contains the name, TIN
2) contains the TIN, salary for the first half of the year, salary for the second half of the year

Here '' print(num[0])'' displays his TIN, tell me how to use this variable to get the name of the employee in another file

def salary():
    fileWorkers = open("C:/Users/OWNER/Desktop/lab/workers.txt","r",encoding="utf-8")
    infile = open("C:/Users/OWNER/Desktop/lab/salary.txt","r",encoding="utf-8")
    arr = []
    arr1 = []
    arrWor=[]
    arrWor_1 = []
    max = 0
    num = 0
    
    arr = infile.readlines()
    for i in arr:
        if i and i != "\n":
         arr1.append(i.split(",")) 

    for summ in arr1:
      if (max<int(summ[1])):
          max = int(summ[1])
          num=summ
    print(num[0])
    
    arrWor = fileWorkers.readlines()
    for i in arrWor:
        if i and i != "\n":
            s = i.split(",")
            b = s[1]         
            if num[0] == b :
                print(s[0]) 


salary()


First file (workers.txt):
Anton M.P., INN: 1234567898
Zukhra M.Yu., INN: 1234567891
Artur O.Yu., INN: 1234567897
Pavlo M.R., INN: 1234567894
Nazar A.V., INN:1234567899
Nadar S.F., INN:1234567892 Maria
L.V., INN:1234567893
Erika M.P., INN:1234567895
Anelya R.Yu., INN:1234567810
Artem V.Yu., INN:1234567

Second file (salary.txt):
1234567898,99424,99524
1234567891,91300,95020
1234567897,91870,98200
1234567894,63400,67510
1234567899,80500,76020
1234567892,68070,65500
1234567893,76320,78000
1234567895,97600,96050
1,234,567,810.98060, 97200
1234567896.78030.83002

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
ink, 2018-02-22
@AskMy

like this: https://codepen.io/topicstarter/pen/JpZdPv

D
Dymok, 2018-02-21
@UnluckySerivelha

Block with text as a background image with WE, position as needed, min-height according to the height of the image with WE

S
Sergey Karbivnichy, 2021-03-20
@hottabxp

Briefly, you can:

with open('salary.txt') as file:
  lines = file.read().splitlines()

with open('workers.txt') as file:
  lines2 = file.read().splitlines()

max_salary = max([x.split(',')[1] for x in lines])
inn = [x.split(',')[0] for x in lines if x.split(',')[1] == max_salary][0]
max_salary_user = [x.split(',')[0] for x in lines2 if x.split(',')[1][4:] == inn][0]

print(max_salary_user)

As far as I understand, constructions like this put you in a stupor: But there is nothing wrong with them. This construction is equivalent to the following:
max_salary = max([x.split(',')[1] for x in lines])
my_list = [] # Создаем пустой список

for x in lines: # перебираем каждую строку
  my_list.append(x.split(',')[1]) # Каждую строку делим по запятым, берем второй элемент и добавляем в список.

print(my_list) # В списке будет ['99424', '91300', '91870', '63400', '80500', '68070', '76320', '97600', '98060', '78030']
print(max(my_list)) # С помощью функции max берем сомое большое число из списка.

Try to practice with the following simple example: There is a list of numbers - from 1 to 15. We need to select numbers that are greater than 7 and less than 13. You can do this:
i = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]

for x in i:
  if x > 7 and x < 13:
    print(x)

And you can make it shorter:
i = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]

num = [x for x in i if x > 7 and x < 13]
print(num)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question