2018-10-22:格式化内容

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)

'''

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,453评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,967评论 0 23
  • 本人承诺,文章内容为原创。 上一篇:未完待续(二) 一 “头要正,颈要直,两眼平视前方,中指紧贴裤缝线.........
    流沙小妖怪阅读 610评论 13 10
  • 七绝•三过大茅坡 前天路过大茅坡,地薄山巉野免多。肩挑背磨唉气叹,草房摞木石支锅。 昨天路过大茅坡,号子炮隆铁臂和...
    苗条的姥姥阅读 244评论 0 0
  • 你还记得 你和他的所有第一次吗 他说,回国后,不要喜欢别人好不好 他说,明年,你会回来陪我的对不对? ...
    视点驿站阅读 367评论 0 0