【问题标题】:How to open and search large txt files in Python/ flask如何在 Python/flask 中打开和搜索大文本文件
【发布时间】:2015-09-22 08:54:18
【问题描述】:

所以我目前正在制作一个烧瓶应用程序,它是语音标记器的一部分,该应用程序的一部分使用几个 txt 文件来检查一个词是名词还是动词,通过查看该词是否在文件。例如,这是我使用的对象:

class Word_Ref (object):
    #used for part of speech tagging, and word look up.

def __init__(self, selection):
    if selection == 'Verbs':
        wordfile = open('Verbs.txt', 'r')
        wordstring = wordfile.read()
        self.reference = wordstring.split()
        wordfile.close()
    elif selection == 'Nouns':
        wordfile = open('Nouns.txt', 'r')
        wordstring = wordfile.read()
        self.reference = wordstring.split()
        wordfile.close()
    elif selection == 'Adjectives':
        wordfile = open('Adjectives.txt', 'r')
        wordstring = wordfile.read()
        self.reference = wordstring.split()
        wordfile.close()
    elif selection == 'Adverbs':
        wordfile = open('Adverbs.txt', 'r')
        wordstring = wordfile.read()
        self.reference = wordstring.split()
        wordfile.close()
    elif selection == 'Pronouns':
        self.reference = ['i', 'me', 'my', 'mine', 'myself', 'you', 'your', 'yours', 'yourself', 'he', 'she', 'it', 'him', 'her'
                          'his', 'hers', 'its', 'himself', 'herself', 'itself', 'we', 'us', 'our', 'ours', 'ourselves',
                          'they', 'them', 'their', 'theirs', 'themselves', 'that', 'this']
    elif selection == 'Coord_Conjunc':
        self.reference = ['for', 'and', 'nor', 'but', 'or', 'yet', 'so']
    elif selection == 'Be_Verbs':
        self.reference = ['is', 'was', 'are', 'were', 'could', 'should', 'would', 'be', 'can', 'cant', 'cannot'
                          'does', 'do', 'did', 'am', 'been']
    elif selection == 'Subord_Conjunc':
        self.reference = ['as', 'after', 'although', 'if', 'how', 'till', 'unless', 'until', 'since', 'where', 'when'
                          'whenever', 'where', 'wherever', 'while', 'though', 'who', 'because', 'once', 'whereas'
                          'before']
    elif selection =='Prepositions':
        self.reference = ['on', 'at', 'in']
    else:
        raise ReferenceError('Must choose a valid reference library.')
def __contains__(self, other):
    if other[-1] == ',':
        return other[:-1] in self.reference
    else:
        return other in self.reference

然后这是我的flask app py文档:

from flask import Flask, render_template, request
from POS_tagger import *

app = Flask(__name__)


@app.route('/', methods=['GET', 'POST'])
def index(result=None):
    if request.args.get('mail', None):
        retrieved_text = request.args['mail']
        result = process_text(retrieved_text)
    return render_template('index.html', result=result)


def process_text(text):
    elem = Sentence(text)
    tag = tag_pronouns(elem)
    tag = tag_preposition(tag)
    tag = tag_be_verbs(tag)
    tag = tag_coord_conj(tag)
    tag = tag_subord_conj(tag)
    tagged = package_sentence(tag)
    new = str(tagged)
    return new


if __name__ == '__main__':
    app.run()

因此,当烧瓶应用程序中的 process_text 函数使用任何使用 open() 然后使用 .read() 的函数时,它会导致一个内部服务器,即使我将它与 Word_Ref 对象一起使用也是如此。另外,我还用一个 3 行的 txt 文件对此进行了测试,它仍然导致相同的内部服务器错误。我的 POS_tagger 的所有其他函数都在烧瓶应用程序中工作,所有这些函数,甚至 open() 都在解释器中工作。

为此目的查看 txt 文件的 open() 方式的任何替代解决方案?

编辑:这里是回溯:

File "/Users/Josh/PycharmProjects/Informineer/POS_tagger.py", line 174, in tag_avna
    adverbs = Word_Ref('Adverbs')
File "/Users/Josh/PycharmProjects/Informineer/POS_tagger.py", line 91, in __init__
    wordfile = open('Adverbs.txt', 'r')
FileNotFoundError: [Errno 2] No such file or directory: 'Adverbs.txt'

txt 文件与烧瓶应用程序位于同一目录中

【问题讨论】:

  • 烧瓶日志说什么?如果您在调试模式下运行应用程序,您将获得 python 回溯:app.run(debug=True) 这些将对您和我们都有帮助
  • ok 贴出来了,奇怪的是txt文件和flask app py文件在同一个目录下。
  • 但这不是 python 解释器的working directory,因为它运行文件。这是find data file 类型的问题。

标签: python http python-3.x flask


【解决方案1】:

也许在你的 Flask app.py 程序中尝试这样的事情:

import os

_dir = os.path.abspath(os.path.dirname(__file__))
adverb_file = os.path.join(_dir, 'Adverbs.txt')

您可能需要根据您希望_dir 指向的位置进行修改,但它会更具动态性。

还可以考虑将Context Manager 用于文件 IO。它会稍微压缩代码,同时保证文件在出现异常等情况下关闭。

例如:

with open(adverb_file, 'r') as wordfile:
    wordstring = wordfile.read()
    self.reference = wordstring.split()

【讨论】:

  • 好的,我添加了你的方法,如果我在 python 控制台中运行它,标记器就可以工作,但是如果我在烧瓶项目中运行它,我会得到这个:文件“/Users/Josh/PycharmProjects/Informineer /POS_tagger.py",第 101 行,在 init 中 wordfile = open(adverb_file, 'r') FileNotFoundError: [Errno 2] No such file or directory: '/Applications/PyCharm.app/Contents/ bin/副词.txt'
  • 由于某种原因,它会进入 pycharm bin 而不是我的项目
  • 实际上,当我通过命令行运行 Flask 应用程序时,一切正常,但不能通过 PyCharm.... :/ 感谢您的帮助。
  • @JoshWeinstein 没问题!很高兴你让它工作了。
猜你喜欢
  • 1970-01-01
  • 2012-07-28
  • 1970-01-01
  • 2019-10-09
  • 2015-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多