• 文件处理练习题


    下面的log变量记录了云服务器上 当天上传的文件信息
    其中第一列是文件名,第二列是文件大小
    
    请编写一个程序,统计出不同类型的 文件的大小总和
    比如:
    jpeg  9988999
    json   324324
    png   2423233
    ----------------------------------
    
    
    log = '''
    f20180111014341/i_51a7hC3W.jpeg	169472	FrITJxleSP7wUD-MWw-phL_KP6Eu	15156063244230469	image/jpeg	0	
    f20180111014341/j_R0Hpl4EG.json	1036	ForGzwzV3e-uR3_UzvppJs1VgfQG	15156064773253144	application/json	0	
    f20180111020739/i_0TDKs0rD.jpeg	169472	FrITJxleSP7wUD-MWw-phL_KP6Eu	15156076847077556	image/jpeg	0	
    f20180111020739/j_JFO6xiir.json	1040	FmUhTchdLOd7LBoE8OXzPLDKcW60	15156077904192983	application/json	0	
    f20180111090619/i_1BwNksbL.jpg	49634	FtXBGmipcDha-67WQgGQR5shEBu2	15156329458714950	image/jpeg	0	
    f20180111090619/i_3BKlsRaZ.jpg	30152	FoWfMSuqz4TEQl5FT-FY5wqu5NGf	15156330575626044	image/jpeg	0	
    f20180111090619/i_5XboXSKh.jpg	40238	Fl84WaBWThHovIBsQaNFoIaPZcWh	15156329453409855	image/jpeg	0	
    f20180111090619/i_6DiYSBKp.jpg	74017	FrYG3icChRmFGnWQK6rYxa88KuQI	15156329461803290	image/jpeg	0	
    f20180111090619/i_76zaF2IM.jpg	38437	Fui8g5OrJh0GQqZzT9wtepfq99lJ	15156334738356648	image/jpeg	0	
    f20180111090619/i_B6TFYjks.jpg	37953	FleWqlK2W1ZmEgAatAEcm1gpR0kC	15156329464034474	image/jpeg	0	
    f20180111090619/i_N9eITqj3.jpg	38437	Fui8g5OrJh0GQqZzT9wtepfq99lJ	15156330419595764	image/jpeg	0	
    f20180111090619/i_QTSNWmA6.jpg	37953	FleWqlK2W1ZmEgAatAEcm1gpR0kC	15156333104224056	image/jpeg	0	
    f20180111090619/i_XdHcAfh1.jpg	56479	FjLQIQ3GxSEHDfu6tRcMylK1MZ05	15156334227270309	image/jpeg	0	
    f20180111090619/i_Xyy723MU.jpg	50076	FsfZpQzqu084RUw5NPYW9-Yfam_R	15156334229987458	image/jpeg	0	
    f20180111090619/i_d8Go0EOv.jpg	30152	FoWfMSuqz4TEQl5FT-FY5wqu5NGf	15156334736228515	image/jpeg	0	
    f20180111090619/i_diuHmX53.jpg	40591	FuTx1pw4idbKnV5MSvNGxCA5L470	15156333878320713	image/jpeg	0	
    f20180111090619/i_qQKzheSH.jpg	55858	Fj0A3i8V7fzzOiPQFL79ao15hkN9	15156329456666591	image/jpeg	0	
    f20180111090619/i_rHL5SYk8.jpg	40238	Fl84WaBWThHovIBsQaNFoIaPZcWh	15156336509742181	image/jpeg	0	
    f20180111090619/i_xZmQxUbz.jpg	40238	Fl84WaBWThHovIBsQaNFoIaPZcWh	15156333240603466	image/jpeg	0	
    f20180111090619/i_zBDNgXDv.jpeg	73616	FlgNwq8lypgsxrWs_ksrS_x47SQV	15156334232887875	image/jpeg	0	
    f20180111090619/j_4mxbEiVh.json	2990	Fpq-3yl3Yr1CadNrJVSDnpeRhQtT	15156331445226898	application/json	0	
    f20180111090619/j_i1K74768.json	3042	Fl5PpDw1TsZXMuhoq1RUrOeGZ6br	15156335067090003	application/json	0	
    f20180111095839/i_Q7KMKeda.png	518522	Fl-yB1_ruL2uxZN9k7DjB62h9dYH	15156359599713253	image/png	0	
    f20180111095839/j_5DpqHolV.json	184	FoYvi7cmSrzuVjUgCRzW5kU95SVo	15156359719719064	application/json	0	
    f20180111100442/i_No8kToIV.jpg	48975	Fu1cw3f--5Vpz9kLGeJfvljhCtyZ	15156364349642377	image/jpeg	0	
    f20180111100442/i_P1bkvSeg.jpg	68200	FvYe8vi46TjUKhEy_UwDqLhO6ZsW	15156363800690634	image/jpeg	0	
    f20180111100442/i_T1AulKcD.jpg	52641	Fj2YzvdC1n_1sF93ZZgrhF3OzOeY	15156364021186365	image/jpeg	0	
    f20180111100442/i_X8d8BN07.jpg	50770	FivwidMiHbogw77lqgkIKrgmF3eA	15156363969737156	image/jpeg	0	
    f20180111100442/i_g0wtOsCX.jpg	76656	Fmtixx0mP9CAUTNosjLuYQHL6k0P	15156363448222155	image/jpeg	0	
    f20180111100442/i_h5OT9324.jpg	72672	FvbIqPLTh2cQHTIBv2akUfahZa_Z	15156364401354652	image/jpeg	0	
    f20180111100442/i_he8iLYI6.jpg	49399	FjeJvwjwhU-hKZsq66UoBg9_tEJs	15156363907932480	image/jpeg	0	
    f20180111100442/i_kg29t7Pp.jpg	76293	FuYj__sSeEN7AsXMbxO24Z8Suh8d	15156364156384686	image/jpeg	0	
    f20180111100442/i_oz1YoBI1.jpg	75620	FkY3xsUMwOI01zgoH1iXXgiQeq6I	15156364089112904	image/jpeg	0	
    f20180111100442/i_xrOT98on.jpg	50021	Fql7ookM1Rc6V7VairKAfnKe-o9w	15156363856357316	image/jpeg	0	
    f20180111135114/i_Zqt8Tmoe.png	161629	FlELw59_mV3VqDBLyu1BKN4fIWnx	15156500155209863	image/png	0	
    f20180111135114/j_uhHoMXKq.json	159	FrypljwAr2LgoLAePBNTUYTUAgDt	15156500200488238	application/json	0	
    f20180111142119/i_s83iZ2GR.png	92278	Fns8tdh3JCkRmfE_COYEu4o8w03E	15156517082371259	image/png	0	
    f20180111142119/j_0g45JRth.json	159	Fq1rFwdRguYRXrp61nGZ5TsUG1V-	15156517143375596	application/json	0	
    f20180111144306/i_yE5TC84E.png	139230	Fjf61ymabEnEvnr5ZMHFjXGCrYlP	15156530038824150	image/png	0	
    f20180111144306/j_OF4WVtSH.json	159	FqwkKcxfo8jd0jFUyuH4X2CrnE9q	15156530083419530	application/json	0	
    f20180111150230/i_KtnER4g3.png	120044	FuwOWdrqzcr2-UScem-LzEMgMezs	15156541734892258	image/png	0	
    f20180111150230/j_xMSUEejY.json	158	FjJr_4deMqFphGaptm-2Pa6wwRP2	15156541771989216	application/json	0	
    f20180111151741/i_JuSWztB3.jpg	92506	FrIjRevHSi6xv4-NQa2wrHu5a1zQ	15156550875370965	image/jpeg	0	
    f20180111153550/i_9wWzVenl.gif	769872	FvslKY9JUaCQm-lu02E34tvAP_oG	15156561674621628	image/gif	0	
    '''
    
    方法一:
    list1 = []
    def func1(leixing,changdu):
    for one in list1:
    if one[0] ==leixing:
    one[1]+= changdu
    break
    else:
    list1.append([leixing,changdu])

    for one in log.split(' '):
    if one.split('.') != ['']:
    type1 = one.split ('.')[1].split(' ')[0]
    int1 =int(one.split('.')[1].split(' ')[1])
    func1(type1,int1)

    print(list1)

    方法二:
    dict2={}

    for one in log.split(' '):
    if one.split('.') != ['']:
    type1 = one.split ('.')[1].split(' ')[0]
    int1 =int(one.split('.')[1].split(' ')[1])
    if type1 not in dict2:
    dict2.update({type1:int1})
    else:
    dict2[type1] += int1

    print(dict2)


    现有文件1(如下,请保存到文件file1.txt中), 记录了公司员工的薪资,其内容格式如下
    
    name: Jack   ;    salary:  12000
     name :Mike ; salary:  12300
    name: Luk ;   salary:  10030
      name :Tim ;  salary:   9000
    name: John ;    salary:  12000
    name: Lisa ;    salary:   11000
    
    每个员工一行,记录了员工的姓名和薪资,
    每行记录 原始文件中并不对齐,中间有或多或少的空格
    
    现要求实现一个python程序,计算出所有员工的税后工资(薪资的90%)和扣税明细,
    以如下格式存入新的文件 file2.txt中,如下所示
    
    name: Jack   ;    salary:  12000 ;  tax: 1200 ; income:  10800
    name: Mike   ;    salary:  12300 ;  tax: 1230 ; income:  11070
    name: Luk    ;    salary:  10030 ;  tax: 1003 ; income:   9027
    name: Tim    ;    salary:   9000 ;  tax:  900 ; income:   8100
    name: John   ;    salary:  12000 ;  tax: 1200 ; income:  10800
    name: Lisa   ;    salary:  11000 ;  tax: 1100 ; income:   9900
    
    要求像上面一样的对齐
    tax 表示扣税金额和 income表示实际收入。注意扣税金额和 实际收入要取整数  

    with open('C:/aaa/file1.txt') as f1,open ('C:/aaa/file2.txt','w') as f2:
    lines = f1.read().splitlines()
    for one in lines:
    a,b = one.split(';')
    name = a.split(':')[1].strip()
    salary = int(b.split(':')[1].strip())
    f2.write(f'name: {name:<7};salary:{salary:>7}; tax:{int(salary/10):>4};income:{int(salary*0.9):>7} ')

    现有一个数据库记录文件(见附件0005_1.txt),保存了学生课程签到的数据库记录。 内容格式如下 ,
    ('2017-03-13 11:50:09', 271, 131),
    ('2017-03-14 10:52:19', 273, 131),
    ('2017-03-13 11:50:19', 271, 126),
    每一行记录保存了学生的一次签到信息。
    每一次签到信息的记录,分为三个部分, 分别是签到时间、签到课程的id号、签到学生的id号
    要求大家实现下面的函数。其中参数fileName 为 数据库记录文件路径, 输出结果是将数据库记录文件中的学生签到信息保存在一个字典对象中,并作为返回值返回。
    def putInfoToDict(fileName):
    要求返回的字典对象的格式是这样的:
    key 是各个学生的id号, value是 该学生的签到信息
       其中value,里面保存着该学生所有签到的信息
           其中每个签到的信息是字典对象,有两个元素: key 是lessonid的 记录课程id,key是checkintime的 记录签到时间
    比如,对于上面的示例中的3条记录,相应的返回结果如下:
    {
    131: [
    {'lessonid': 271,'checkintime':'2017-03-13 11:50:09'},
    {'lessonid': 273,'checkintime':'2017-03-14 10:52:19'},
    ],


    126: [
    {'lessonid': 271,'checkintime':'2017-03-13 11:50:19'},
    ],

    }

    dict2 = { }
    with open('C:/aaa/0005_1.txt') as f:
    for one in f.read().splitlines():
    one1 = (one.replace('(','').replace(')','').replace('/t','').strip().replace("'",''))
    one2 = one1.strip(',').strip(';')
    #print(one2)
    citime,lessonid,studentid=one2.split(',')
    lessonid = int(lessonid.strip())
    studentid = studentid.strip()
    dict1={'lessonid':lessonid,'checkintime':citime}
    if studentid not in dict2:
    dict2[studentid]=[]
    dict2[studentid].append(dict1)
    import pprint
    pprint.pprint(dict2)













  • 相关阅读:
    http状态大全
    goaccess 分析日志
    laravel 初识
    mac 安装wget
    python 16进制转成中文
    zeromq安装---php-zmq
    Mac Yosemite 10.10 编辑mac的配置
    Mac Yosemite 10.10 下利用 Launchctl 自启动 mysql
    MacBook设置终端命令行的颜色高亮显示
    XPath 常用语法札记
  • 原文地址:https://www.cnblogs.com/hyzhang/p/13840493.html
Copyright © 2020-2023  润新知