python pickle.load在Windows平台上运行不正常

时间:2016-05-19 07:58:11

标签: python-2.7 pickle

当我运行以下代码时

libraryDependencies += "org.apache.phoenix" % "phoenix-core" % "4.5.1-HBase-1.0"

追溯显示:

import pickle


class PlayerData:
    def __init__(self, user_name, pwd):
        self.name = user_name
        self.pwd = pwd
        self.pos = 12
        self.online = False
        self.info = {'gunNum': 0,
                     'guns': None,
                     'barrelNum': 0,
                     'barrels': None,
                     'health': 100,
                     'ammos': {'0': {'ammoCurrentCarried': 50,
                                     'ammoName': 'Assault Rifle'},
                                '1': {'ammoCurrentCarried': 30, 'ammoName': 'Assault Rifle RUS'},
                                'number': 2
                               }
                     }


def create_user_file(user):
    file_path = "data/users/" + user.name
    f = open(file_path, "w")
    pickle.dump(user, f, pickle.HIGHEST_PROTOCOL)
    f.close()


def load_user(user_name):
    file_path = "data/users/" + user_name
    f = open(file_path, "r")
    user = pickle.load(f)
    f.close()
    return user


def register(user_name):
        pwd = "ab"
        data = PlayerData(user_name, pwd)
        create_user_file(data)


def login(user_name):
    user = load_user(user_name)
    print user.__dict__

name = "dd"
register(name)
login(name)

但如果我在Mac上运行以前的代码,它可以正常工作。

另一个问题是,如果我删除Traceback (most recent call last): File "D:/pycharm/hello/hello.py", line 50, in <module> login(name) File "D:/pycharm/hello/hello.py", line 45, in login user = load_user(user_name) File "D:/pycharm/hello/hello.py", line 33, in load_user user = pickle.load(f) File "C:\Python27\lib\pickle.py", line 1378, in load return Unpickler(file).load() File "C:\Python27\lib\pickle.py", line 858, in load dispatch[key](self) File "C:\Python27\lib\pickle.py", line 1169, in load_binput i = ord(self.read(1)) TypeError: ord() expected a character, but string of length 0 found 的某些属性,它可以在Windows和Mac上运行。例如,

PlayerData

我不知道如何解决这个问题

1 个答案:

答案 0 :(得分:3)

尝试将<template name="prodlist"> <select id="category-select" disabled= {{ shouldBeDisabled }}> <option selected="selected">Please Select</option> {{#each prodlist}} <option value="{{this}}">{{this}}</option> {{/each}} </select> </template> 替换为'r',将'rb'替换为'w'。请参阅这个古老的Python错误报告:https://mail.python.org/pipermail/python-bugs-list/2005-April/028565.html