Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中使用sys.stdin.read()_Python_Peewee - Fatal编程技术网

如何在python中使用sys.stdin.read()

如何在python中使用sys.stdin.read(),python,peewee,Python,Peewee,一、 我试图使用peewee模块将用户输入的多个文本保存到数据库中,但当我在控制台中按ctrl+d时,它会给我EOFError。我想问题是whit sys.stdin.read()。无论如何,有人能帮我解决这个问题吗? 代码如下: #!/user/bin/env python3 from peewee import * import sys import datetime from collections import OrderedDict db = SqliteDatabase('dia

一、 我试图使用peewee模块将用户输入的多个文本保存到数据库中,但当我在控制台中按ctrl+d时,它会给我EOFError。我想问题是whit sys.stdin.read()。无论如何,有人能帮我解决这个问题吗? 代码如下:

#!/user/bin/env python3
from peewee import *

import sys
import datetime
from collections import OrderedDict

db = SqliteDatabase('diary.db')


class Entry(Model):
    content = TextField()
    timestamp = DateTimeField(default=datetime.datetime.now)  # no ()

    class Meta:
        database = db

def intitalize():
    '''create database and table if not exists'''
    db.connect()
    db.create_tables([Entry], safe=True)

def menu_loop():
    '''show menu'''
    choice = None

    while choice != 'q':
        print("enter 'q' to quit")
        for key, value in menu.items():
            print('{}) {}'.format(key, value.__doc__))
        choice = input('action: ').lower().strip()

        if choice in menu:
            menu[choice]()

def add_entry():
    '''add an entry'''
    print("Enter your entry. press ctrl+d when finished")
    data = sys.stdin.read().strip()

    if data:
        if input('Save Entry?[Yn]').lower()!='n':
            Entry.create(content=data)
            print('saved successfully')

def view_entry():
    '''view entries'''

def delete_entry():
    '''delete an entry'''


menu = OrderedDict([
    ('a', add_entry),
    ('v', view_entry),
])


if __name__ == '__main__':
    intitalize()
    menu_loop()   
以下是我在pycharm中遇到的错误:

enter 'q' to quit
a) add an entry
v) view entries
action: a
Enter your entry. press ctrl+d when finished
some text
and more
^D
Save Entry?[Yn]Traceback (most recent call last):
  File "C:/Users/Xylose/Desktop/small lab/peewee/venv/dairy.py", line 61, in <module>
    menu_loop()
  File "C:/Users/Xylose/Desktop/small lab/peewee/venv/dairy.py", line 34, in menu_loop
    menu[choice]()
  File "C:/Users/Xylose/Desktop/small lab/peewee/venv/dairy.py", line 42, in add_entry
    if input('Save Entry?[Yn]').lower()!='n':
EOFError: EOF when reading a line

Process finished with exit code 1
输入“q”退出
a) 添加条目
v) 查看条目
行动:a
输入您的条目。完成后按ctrl+d
一些文本
还有更多
^D
保存条目?[Yn]回溯(最近一次呼叫最后一次):
文件“C:/Users/Xylose/Desktop/small lab/peewee/venv/dairy.py”,第61行,在
菜单_循环()
文件“C:/Users/Xylose/Desktop/small lab/peewee/venv/dairy.py”,第34行,在菜单循环中
菜单[选择]()
文件“C:/Users/Xylose/Desktop/small lab/peewee/venv/dairy.py”,第42行,在add_条目中
如果输入('Save Entry?[Yn]')。下限()n':
EOF:读取一行时的EOF
进程已完成,退出代码为1
在Python中

EOFError: EOF when reading a line 
此错误有两个原因

1.以错误的方式/格式读取文件

 import sys
 for line in sys.stdin:
     print (line)
这就是我们如何使用“sys.stdln”进行读取

2.如果文件在Python中损坏,则有可能出现相同的错误

EOFError: EOF when reading a line 
此错误有两个原因

1.以错误的方式/格式读取文件

 import sys
 for line in sys.stdin:
     print (line)
这就是我们如何使用“sys.stdln”进行读取


2.如果文件在正常情况下允许Ctrl-D后从stdin读取时被损坏,则有可能出现同样的错误,但我只在Ubuntu上测试过这一点(类似于您的代码工作得非常好)。我发现这是在Windows上运行的,Windows控制台的行为可能会有所不同,并在Ctrl-D之后拒绝任何read()操作。一种可能的解决方案是使用try/except语句捕获
eoferor
异常,并在异常发生时关闭并重新打开sys.stdin。大概是这样的:

# note: check that sys.stdin.isatty() is True!

try:
    # read/input here

except EOFError:

    sys.stdin.close()
    sys.stdin = open("con","r")
    continue # or whatever you need to do to repeat the input cycle

通常允许在Ctrl-D之后读取stdin,但我只在Ubuntu上进行了测试(类似于您的代码工作得非常好)。我发现这是在Windows上运行的,Windows控制台的行为可能会有所不同,并在Ctrl-D之后拒绝任何read()操作。一种可能的解决方案是使用try/except语句捕获
eoferor
异常,并在异常发生时关闭并重新打开sys.stdin。大概是这样的:

# note: check that sys.stdin.isatty() is True!

try:
    # read/input here

except EOFError:

    sys.stdin.close()
    sys.stdin = open("con","r")
    continue # or whatever you need to do to repeat the input cycle