python - 如何将其作为列表阅读?

标签 python string list error-handling

我需要扫描代码中的第二项以检查其正确性,但返回此错误:

Traceback (most recent call last):
  File "C:/Users/oscar/Documents/ff.py", line 29, in <module>
    with open(account) as f:
TypeError: expected str, bytes or os.PathLike object, not list

编码:
step = str(input("do you have an account? (reply with yes or no)"))

if step == ("no"):
    username = input("what do you want your username to be?")
    password = input("what do you want your password to be?")
    favgenre = input("what is your favourite genre?")
    favartist = input("who is your favourite artist?")
    account = open("%s.txt" %username, "w+")
    account.write(username)
    account.write(",")
    account.write(password)

    account.write(",")
    account.write(favgenre)
    account.write(",")
    account.write(favartist)
    account.write(",")
    account.close()

if step == ("yes"):
    username = input("please enter username")
    password = input("please enter your password")
    account = open("%s.txt" %username, "a+")
    account = [account]
    filename = (username)

    if username == username:
        with open(account) as f:
            strings = f.readlines()
            data = [string.split() for string in strings]
            print(data[1])

最佳答案

我会使用 pickle 模块来保存数据,而不是为每个用户创建单独的文件,我会将它们全部保存在字典中并保存。

这就是我的意思:

import pickle

# First read any existing database into memory.
#
database_filename = 'user_db.pkl'
db_updated = False  # Flag indicating database needs to rewritten.
# Read the existing database if any, otherwise create an empty version.
try:
    with open(database_filename, 'rb') as infile:
        database = pickle.load(infile)
except FileNotFoundError:
    database = {}
    db_updated = True

# Then interact with user or do other operations to it.
#
step = str(input("Do you have an account (yes or no)? "))

if step == "no":
    username = input("What do you want your username to be? ")
    password = input("What do you want your password to be? ")
    favgenre = input("What is your favourite genre? ")
    favartist = input("Who is your favourite artist? ")

    data = [password, favgenre, favartist]
    database[username] = data
    db_updated = True

elif step == "yes":
    username = input("Please enter your username: ")
    if username not in database:
        print('Error: username {!r} not found in database'.format(username))
    else:
        password = input("OK, please enter your password: ")

        data = database.get(username)
        if data[0] != password:
            print('Error: password does not match')
            del data
        else:
            print('username {!r} found, data: {}'.format(username, data))

else:
    print('Please enter "yes" or "no"')

if db_updated:
    with open(database_filename, 'wb') as outfile:
        pickle.dump(database, outfile, pickle.HIGHEST_PROTOCOL)
        print('Database file updated')

还有关于 pickle 的信息在我的answer提问 Saving an Object (Data persistence) .

关于python - 如何将其作为列表阅读?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48140773/

相关文章:

ios - 适用于 Flex mobile\iOS 的轻量级数据网格组件?

python - 如何让我的 matplotlib 图超出坐标轴?

python - 在python中跨多个进程共享类变量

python 用 "on duplicate key update"执行很多?

python - 将值 append 到 Python 字典

java - 将字符串拆分为多行?

java - 在 Java 中将较大的集合(集合、数组、列表)拆分为较小的集合,并跟踪最后一个返回的集合

json - R:将具有空元素的嵌套列表转换为 data.frame(来自 json)

c++ - 从文件中读取返回一个我没想到的结果,试图理解为什么

java - 替换包含正则表达式的行