美文网首页自动化学习-python
2018-10-22:格式化内容

2018-10-22:格式化内容

作者: 种树在此时 | 来源:发表于2018-10-31 00:08 被阅读0次

def putinfotodict(infile):
outstr = {}
with open(infile) as inf:
for one in inf.read().split(',\n'):
stuid = one.split(',')[-1]
stuid = stuid[:-1]
claid = one.split(',')[-2]
atttime1 = one.split(',')[-3]
atttime = atttime1[1:]
valueandvalue = {}
valueandvalue['lessonid'] = claid
valueandvalue['checkintime'] = atttime

        if stuid not in outstr:
            outstr[stuid] = []
        outstr[stuid].append(valueandvalue)

    return outstr

infile = 'file2.txt'
p=putinfotodict(infile)

print(p)

import pprint
pprint.pprint(p)

'''

標準答案

def putinfotodict(filename):
redic={}
with open(filename) as f:
lines=f.read().splitlines()
for line in lines:
# remove '('and ')'
line=line.replace('(','').replace(')','').replace(';','').strip()
parts=line.split(',')
citime=parts[0].strip().replace("'",'')
lessonid=int(parts[1].strip())
userid=int(parts[2].strip())
toadd={'lessonid':lessonid,'checkintime':citime}
#if not in,need to create list first
if userid not in redic:
redic[userid]=[]
redic[userid].append(toadd)
#or just
## redic.setdefault(userid,[]).append(toadd)
return redic

infile = 'file2.txt'
p=putinfotodict(infile)

import pprint
pprint.pprint(p)

'''

相关文章

  • 2018-10-22:格式化内容

    def putinfotodict(infile):outstr = {}with open(infile) as...

  • 2018-10-22

    2018-10-22

  • 2018-10-22

    2018-10-22 孔琳清 2018-10-22 21:03 · 字数 865 · 阅读 0 · 日记本 相信生...

  • One Building

    2018-10-22 途经望京

  • 输出

    目标 格式化输出格式化符号f-字符串 print的结束符 输出 作用:程序输出内容给用户 一. 格式化输出 所谓...

  • 基因、文化与性别

    2018-10-22 第5章 基因、文化与性别 本书第一编“社会思维”包括第2、3、4共四章的内容就学完了,分别讲...

  • IDEA格式化代码与jQuery.trim()方法

    1.格式化前: 2.格式化后: 3.td标签的原有内容尾部多了空格,导致我们使用AJAX获取的数据在格式化代码前后...

  • 基础-15、磁盘格式化+挂载+手动swap

    笔记内容:4.5/4.6 磁盘格式化4.7/4.8 磁盘挂载4.9 手动增加swap空间 一、磁盘格式化-系统文件...

  • awk

    awk:报告生成器,格式化文本输出 内容: awk介绍 awk基本用法 awk变量 awk格式化 awk操作符 a...

  • python3基础

    注释 python中使用‘#’注释一行代码,多行注释使用""" """ 格式化输出 %s 格式化的内容是字符串类型...

网友评论

    本文标题:2018-10-22:格式化内容

    本文链接:https://www.haomeiwen.com/subject/nwqotqtx.html