我应该为sum
中提到的每位员工计算salaries
和bonuses
中的salary.txt
,我编写了以下代码,但没有得到预期的结果结果,其中salary
+ bonus
= total
。
salary.txt
salary =
Ravindra Jadeja=117000
Ambati Rayudu=78000
M S Dhoni=150000
Axar Patel=10100
R Ashwin=130000
Ishant Sharma=135000
Ajinkya Rahane=134000
Shikhar Dhawan=124000
Bhuvneshwar Kumar=115000
Mohammed Shami=102000
Umesh Yadav=140000
Rohit Sharma=120000
Stuart Binny=111000
Suresh Raina=98000
Virat Kohli=135000
bonus =
M S Dhoni=15000
Virat Kohli=12500
Ajinkya Rahane=12500
Shikhar Dhawan=12500
Rohit Sharma=15000
Stuart Binny=15000
Suresh Raina=15000
Ravindra Jadeja=12500
Ambati Rayudu=15000
Axar Patel=9000
R Ashwin=13000
Bhuvneshwar Kumar=12500
Mohammed Shami=10000
Umesh Yadav=20000
Ishant Sharma=15000
示例:
Ravindra Jadeja=117000 + Ravindra Jadeja=12500
示例输出:
Ravindra Jadeja: 129500
代码:
name1 = []
name2 = []
global sal
import itertools
salary = []
salary2 = []
global rep , part
read = open("C:\Users\ishaan_sathe\Documents\chotu2.txt")
for lines in itertools.islice(read ,17 ,None):
lines = lines.strip()
lines = lines.replace("salary =",'')
#print lines
spl = lines.partition("=")
part = spl[0] + spl[2]
name1.append(spl[0])
salary.append(spl[2])
read.close()
read = open("C:\Users\ishaan_sathe\Documents\chotu2.txt")
for line in itertools.islice(read , None):
line = line.replace("bonus =",'')
p = line.partition("=")
f = p[ 0] + p[2]
name2.append(p[0])
salary2.append(p[2].strip())
#print salary
#print salary2
for y,z in zip(salary,salary2):
for x in name1:
for a in name2:
print (x,a)
print (y,z)
if x == a:
try:
sal = x + "total sal = " ,int (y) + int (z)
print sal
except Exception as e:
print e
答案 0 :(得分:0)
不确定我是否了解您,但我想您想总结一下他们的薪水和奖金?
salaries = {}
bonuses = {}
with open('salary.txt','r') as salary:
section = 0
for line in salary:
line = line.strip()
line = line.split("=")
if line[0].strip() == "bonus": section = 0;continue
elif line[0].strip() == "salary": section = 1;continue
if line in (""," "): continue
if section == 0:
try: bonuses[line[0]] = line[1]
except: pass
elif section == 1:
try: salaries[line[0]] = line[1]
except: pass
totals = {}
for key, value in salaries.iteritems():
try:
totals[key] = int(bonuses.get(key, 0)) + int(value)
except:
pass
print totals
Salary.txt
salary =
Ravindra Jadeja=117000
Ambati Rayudu=78000
M S Dhoni=150000
Axar Patel=10100
R Ashwin=130000
Ishant Sharma=135000
Ajinkya Rahane=134000
Shikhar Dhawan=124000
Bhuvneshwar Kumar=115000
Mohammed Shami=102000
Umesh Yadav=140000
Rohit Sharma=120000
Stuart Binny=111000
Suresh Raina=98000
Virat Kohli=135000
bonus =
M S Dhoni=15000
Virat Kohli=12500
Ajinkya Rahane=12500
Shikhar Dhawan=12500
Rohit Sharma=15000
Stuart Binny=15000
Suresh Raina=15000
Ravindra Jadeja=12500
Ambati Rayudu=15000
Axar Patel=9000
R Ashwin=13000
Bhuvneshwar Kumar=12500
Mohammed Shami=10000
Umesh Yadav=20000
Ishant Sharma=15000
输出:
{
'Shikhar Dhawan': 136500,
'Ajinkya Rahane': 146500,
'Suresh Raina': 113000,
'Ishant Sharma': 150000,
'Stuart Binny': 126000,
'Virat Kohli': 147500,
'Ambati Rayudu': 93000,
'Umesh Yadav': 160000,
'Ravindra Jadeja': 129500,
'Bhuvneshwar Kumar': 127500,
'M S Dhoni': 165000,
'Axar Patel': 19100,
'R Ashwin': 143000,
'Rohit Sharma': 135000,
'Mohammed Shami': 112000
}