员工薪资如下:
马云,销售,20000,80000
王健林,市场,64000,35000
马化腾,研发,88000,15000
第三列是底薪,第四列是提成,求月总收入最高的金额是多少
from functools import reduce
f=open("薪资. txt", 'r', encoding ='utf-8')
text =f.read()
f.close()
lines=text. split('\n')
lines
我按这个上面的代码没能够把它们拆开成三个字典,要怎么改才可以拆开,算月收入最高的金额呢?请赐教
from functools import reduce
with open("薪资.txt", 'r', encoding='utf-8') as f:
text = f.read()
lines = text.split('\n')
employees = {}
for line in lines:
if not line:
continue
name, department, salary, bonus = line.split(',')
salary = int(salary)
bonus = int(bonus)
total_income = salary + bonus
employees[name] = total_income
highest_income = reduce(lambda x, y: x if employees[x] > employees[y] else y, employees.keys())
print("月总收入最高的员工是%s,月总收入为%d元" % (highest_income, employees[highest_income]))
使用readline()函数,一行行地读取就好了
目标页面:http://quote.stockstar.com/fund/stock_3_1_X.html ,X表示页数
具体数据位于一个表格中
一共有37页,采用正则截取的方式,最后也是写入csv
from GetStockList import getHtml
import re
import csv
import threading
def getFundsInfo(baseUrl):
html = getHtml(baseUrl)
reslist = re.findall("<tbody[\s\S]*</tbody>",html)
tbody = reslist[0]
reslist = re.findall('>(\S+?)</',tbody)
for i in range(0,len(reslist),8):
rowList = []
for j in range(8):
rowList.append(reslist[i+j])
writer.writerow(rowList)
if __name__ == '__main__':
f = open('D:/Py/StockFunds.csv', 'w', encoding='utf-8', newline="")
writer = csv.writer(f)
writer.writerow(('基金代码', '基金名称', '单位净值', '累计净值', '日增长额', '日增长率', '申购', '赎回'))
for page in range(1,38):
baseUrl = 'http://quote.stockstar.com/fund/stock_3_1_{}.html'.format(page)
threading.Thread(target=getFundsInfo,args=(baseUrl,)).start()
注意第一个正则中,因为是html代码所以存在换行符,. 表示任何不包括换行符的字符,所以用[\S\s],\s匹配\t\n\r\f\v中的一个
然后就可以进行数据分析了