zoukankan      html  css  js  c++  java
  • python pickle

    # Author:Alex
    # Date:2017.0605
    # Version:3.6.0
    man = []
    other = []
    #打开note文件,循环内容,并判断,加入到man and other中
    import pickle

    try:
    data = open('note.txt')
    for each_line in data:
    try:
    (role, line_spoken) = each_line.split(':', 1)
    line_spoken = line_spoken.strip()
    if role == 'Man':
    man.append(line_spoken)
    elif role == 'Other man':
    other.append(line_spoken)
    # print (role, end='')
    # print ('said: ', end='')
    # print (line_spoken, end='')
    except ValueError:
    pass
    data.close()
    except IOError:
    print ('The data file is missing')

    # try:
    # man_file = open('man_data.txt','w+')
    # other_file = open('other_data.txt','w+')
    #
    # print_lol(man,fh=man_file)
    # print_lol(other,fh=other_file)
    #
    # except IOError as err:
    # print ('File error:' + str(err))
    # finally:
    # if 'man_file' in locals():
    # man_file.close()
    # if 'other_file' in locals():
    # other_file.close()
    #代替try+except+finally,使用with,将note内容分开,导入两个文件中,man_data and other_data中
    try:
    with open('man_data.txt',"wb") as man_file, open('other_data.txt',"wb") as other_file:
    # print(man, file=man_file)
    # print(other, file=other_file)
    # cycle.print_lol(man, fh=man_file)
    # cycle.print_lol(other, fh=other_file)
    pickle.dump(man,man_file)
    pickle.dump(other,other_file)

    except IOError as err:
    print ('File error:' + str(err))
    except pickle.PickleError as perr:
    print ('Pickling error:' + str(perr))



  • 相关阅读:
    Android Studio轻松上手指南(1)
    在MyEclipse下创建Java Web项目 入门(图文并茂)经典教程
    opencv提取截获图像(总结摘来)
    10.正则表达式匹配
    遍历 Map 的方式
    09.回文数
    08.字符串转换位整数
    07.整数反转
    06. Z字型变换
    05. 求最长回文子串
  • 原文地址:https://www.cnblogs.com/alex-note/p/6945062.html
Copyright © 2011-2022 走看看