接受用户输入以读取文件中的这么多行 - python

我正在尝试接受用户输入以将许多行读入文本文件。到目前为止,我已经收到了这个错误:
在write_dogs line = f.next()。strip()中AttributeError:'_io.TextIOWrapper'对象没有属性'next'

到目前为止,这是我的代码。

def write_dogs():
    total_cards = int(input("How many cards do you wish to play with? "))
    if total_cards %2 != 0:
        print("Please enter an even number")
        main_menu()
    elif total_cards > 30 or total_cards < 4:
        print("Please enter a number less that 30 and more than 4")
        main_menu()
    else:
        N = total_cards
        with open("dogs.txt") as f:
            with open("dogswrite.txt", "w") as f1:
                for i in range(N):
                    line=f.next().strip()
                    f1.write(line)

任何帮助,将不胜感激

工作代码如下

def write_dogs():
    total_cards = int(input("How many cards do you wish to play with? "))
    if total_cards %2 != 0:
        print("Please enter an even number")
        main_menu()
    elif total_cards > 30 or total_cards < 4:
        print("Please enter a number less that 30 and more than 4")
        main_menu()
    else:
        N = total_cards
        with open("dogs.txt") as f:#opens needed file
            with open("dogswrite.txt", "w") as f1:#my own file to later create a class
                for i in range(N):
                    line=next(f).strip()#lists the input number of results each on a new line
                    line = line + "\n"
                    f1.write(line)

参考方案

.next()在这里不起作用,因为f不是列表,而是文件。您应该改用f.readline()

另外,将for循环更改为此:

for i in range(N):
    line=next(f).strip() + ', '
    f1.write(line)

编辑:没注意到索引行,还根据您的评论添加了逗号

Python-crontab模块 - python

我正在尝试在Linux OS(CentOS 7)上使用Python-crontab模块我的配置文件如下:{ "ossConfigurationData": { "work1": [ { "cronInterval": "0 0 0 1 1 ?", "attribute&…

Python:检查是否存在维基百科文章 - python

我试图弄清楚如何检查Wikipedia文章是否存在。例如,https://en.wikipedia.org/wiki/Food 存在,但是https://en.wikipedia.org/wiki/Fod 不会,页面只是说:“维基百科没有此名称的文章。”谢谢! 参考方案 >>> import urllib >>> prin…

Python GPU资源利用 - python

我有一个Python脚本在某些深度学习模型上运行推理。有什么办法可以找出GPU资源的利用率水平?例如,使用着色器,float16乘法器等。我似乎在网上找不到太多有关这些GPU资源的文档。谢谢! 参考方案 您可以尝试在像Renderdoc这样的GPU分析器中运行pyxthon应用程序。它将分析您的跑步情况。您将能够获得有关已使用资源,已用缓冲区,不同渲染状态上…

Python Pandas导出数据 - python

我正在使用python pandas处理一些数据。我已使用以下代码将数据导出到excel文件。writer = pd.ExcelWriter('Data.xlsx'); wrong_data.to_excel(writer,"Names which are wrong", index = False); writer.…

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…