如何读写pickled信息
How to read and write pickled information
我是 Python 的初学者,一直在尝试使用循环和 pickle
.
将用户名和密码写入列表中的文件
但是,我收到错误消息:
NameError: name 'listusername' is not defined
对于以下代码行:
listusername.write("%s\n" % i)
这是我的其余代码:
userName = input('Please enter a username:\n')
password1 = input('Please enter a password:\n')
user_names=[]
passwords=[]
user_names.append(userName)
passwords.append(password1)
for i in user_names:
listusername.write("%s\n" % i)
for i in user_names:
listusername.read("%s\n" % i)
for i in passwords:
listpassword.write("%s\n" % i)
for i in passwords:
listpassword.read("%s\n" % i)
所有答案和建议将不胜感激。
在你的代码中
for i in user_names:
listusername.write("%s\n" % i)
Python 不知道 listusername 是什么。这就是
出错的原因
NameError: name 'listusername' is not defined
要解决此问题,您可以在使用前先设置 listusername,例如
listusername = open('output.txt', 'w')
for i in user_names:
listusername.write("%s\n" % i)
但是,此步骤完全没有必要 - 因为您的问题涉及 pickle 模块 - 其中文档附带 example。这是您的问题的解决方案:
userName = input('Please enter a username:\n')
password1 = input('Please enter a password:\n')
user_names=[]
passwords=[]
user_names.append(userName)
passwords.append(password1)
import pickle
with open('user_names.txt', 'w') as f:
pickle.dump(f, user_names)
with open('passwords.txt', 'w') as f:
pickle.dump(f, passwords)
从您的代码看来,您希望 listusername
成为一个文件对象,让您可以写入字符串,然后读取,呃,一些东西。我不明白listusername.read("%s\n" % i)
,我怀疑你也不明白。
看,我明白了。你很困惑。你是一个新手程序员。让我来帮你。
如果你运行这个Python脚本,它会要求你提供一些帐户信息,pickle它,并将它写入一个名为accounts.pickled
.
的文件
import pickle
accounts = []
# Let's collect some account information.
username = input('Please enter a username: ')
password = input('Please enter a password: ')
account = (username, password) # This tuple represents an account.
accounts.append(account) # Add it to the list of accounts.
# Write the pickled accounts to a file.
with open('accounts.pickled', 'wb') as out_file:
pickle.dump(accounts, out_file)
请注意,我们正在写一个名为 accounts
的数组,它只包含一个帐户。我将把它留作练习,供您弄清楚如何收集有关多个帐户的信息。
稍后,当您想从文件中读取帐户时,可以运行以下脚本。
import pickle
# Unpickle the accounts.
with open('accounts.pickled', 'rb') as in_file:
accounts = pickle.load(in_file)
# Display the accounts one at a time.
for account in accounts:
username, password = account
print('username: %s, password: %s' % (username, password))
我是 Python 的初学者,一直在尝试使用循环和 pickle
.
但是,我收到错误消息:
NameError: name 'listusername' is not defined
对于以下代码行:
listusername.write("%s\n" % i)
这是我的其余代码:
userName = input('Please enter a username:\n')
password1 = input('Please enter a password:\n')
user_names=[]
passwords=[]
user_names.append(userName)
passwords.append(password1)
for i in user_names:
listusername.write("%s\n" % i)
for i in user_names:
listusername.read("%s\n" % i)
for i in passwords:
listpassword.write("%s\n" % i)
for i in passwords:
listpassword.read("%s\n" % i)
所有答案和建议将不胜感激。
在你的代码中
for i in user_names:
listusername.write("%s\n" % i)
Python 不知道 listusername 是什么。这就是
出错的原因NameError: name 'listusername' is not defined
要解决此问题,您可以在使用前先设置 listusername,例如
listusername = open('output.txt', 'w')
for i in user_names:
listusername.write("%s\n" % i)
但是,此步骤完全没有必要 - 因为您的问题涉及 pickle 模块 - 其中文档附带 example。这是您的问题的解决方案:
userName = input('Please enter a username:\n')
password1 = input('Please enter a password:\n')
user_names=[]
passwords=[]
user_names.append(userName)
passwords.append(password1)
import pickle
with open('user_names.txt', 'w') as f:
pickle.dump(f, user_names)
with open('passwords.txt', 'w') as f:
pickle.dump(f, passwords)
从您的代码看来,您希望 listusername
成为一个文件对象,让您可以写入字符串,然后读取,呃,一些东西。我不明白listusername.read("%s\n" % i)
,我怀疑你也不明白。
看,我明白了。你很困惑。你是一个新手程序员。让我来帮你。
如果你运行这个Python脚本,它会要求你提供一些帐户信息,pickle它,并将它写入一个名为accounts.pickled
.
import pickle
accounts = []
# Let's collect some account information.
username = input('Please enter a username: ')
password = input('Please enter a password: ')
account = (username, password) # This tuple represents an account.
accounts.append(account) # Add it to the list of accounts.
# Write the pickled accounts to a file.
with open('accounts.pickled', 'wb') as out_file:
pickle.dump(accounts, out_file)
请注意,我们正在写一个名为 accounts
的数组,它只包含一个帐户。我将把它留作练习,供您弄清楚如何收集有关多个帐户的信息。
稍后,当您想从文件中读取帐户时,可以运行以下脚本。
import pickle
# Unpickle the accounts.
with open('accounts.pickled', 'rb') as in_file:
accounts = pickle.load(in_file)
# Display the accounts one at a time.
for account in accounts:
username, password = account
print('username: %s, password: %s' % (username, password))