【问题标题】:How to loop, print and save outputs in an array如何在数组中循环、打印和保存输出
【发布时间】:2017-06-18 03:12:23
【问题描述】:

我是 Python 的新用户,正在尝试循环以下内容:

text = open('filename.txt', 'rU').read()  
splitter = Splitter()
postagger = POSTagger()
splitted_sentences = splitter.split(text)
pos_tagged_sentences = postagger.pos_tag(splitted_sentences)
dicttagger = DictionaryTagger([ 'dicts/positive.yml', 'dicts/negative.yml'])
dict_tagged_sentences = dicttagger.tag(pos_tagged_sentences)
scoreposneg = sentiment_score(dict_tagged_sentences)
dicttagger = DictionaryTagger([ 'dicts/positive.yml', 'dicts/negative.yml', 'dicts/inc.yml', 'dicts/dec.yml', 'dicts/inv.yml'])
dict_tagged_sentences = dicttagger.tag(pos_tagged_sentences)
scoretotal = sentiment_total(dict_tagged_sentences)
print scoretotal

在此之前,我已经为 Splitter、POSTagger、DictionaryTagger、sentiment_total 设置了类,还创建了 5 个不同的字典。当我在 Python 上为 1 个文件运行它时,这很有效(我从 print scoretotal 中得到了一个数字)。但是,当我尝试创建一个循环并打印所有输出(我的目录中有 650 个文件)时,它不起作用(没有打印任何内容)并且 scoretext.txt 文件中有 0.0。

path = '/mydirectory'
files = glob.glob(path)
for file in files:
    text = open(file, 'rU').read()
    splitter = Splitter()
    postagger = POSTagger()
    splitted_sentences = splitter.split(text)
    pos_tagged_sentences = postagger.pos_tag(splitted_sentences)
    dicttagger = DictionaryTagger([ 'dicts/positive.yml', 'dicts/negative.yml'])
    dict_tagged_sentences = dicttagger.tag(pos_tagged_sentences)
    scoreposneg = sentiment_score(dict_tagged_sentences)
    dicttagger = DictionaryTagger([ 'dicts/positive.yml', 'dicts/negative.yml', 'dicts/inc.yml', 'dicts/dec.yml', 'dicts/inv.yml'])
    dict_tagged_sentences = dicttagger.tag(pos_tagged_sentences)
    scoretotal = sentiment_total(dict_tagged_sentences)
    print scoretotal

scoretotal = np.zeros((1,650))
scoretotal_no = 0
scoretotal_no = scoretotal_no + 1

np.savetxt("scoretext.txt", scoretotal, delimiter=" ", fmt="%s")

如果有人能就此提供一些见解,我将不胜感激。谢谢!

【问题讨论】:

  • 我不确定我理解这应该做什么(scoretotal 在循环外被重置?)但是在for file in files: 之前从print(files) 得到什么?另外,file 是 Python 2.x 中的内置函数,所以我会选择其他名称。
  • 嗨 Roganjosh,我得到的是 [] 。我已将文件更改为文件名。我以为我正在将 650 分数保存到一个数组中,所以它必须脱离循环
  • 好的,首先循环本身不起作用,因为path = '/mydirectory'files = glob.glob(path) 中为您提供了一个空列表(没有什么可以迭代的)。这孤立了一个问题。但是scoretotal = np.zeros((1,650)) 完全破坏了您在scoretotal 的循环期间可能积累的任何数据。事实上,每个循环只会破坏上一次迭代的数据(我假设,但我不知道sentiment_total() 做了什么)。
  • 我已经定义了sentiment_total (def sentence_total(review): return sum([sentence_score(sentence, None, 0.0) for sentence in review])) 返回文本的分数。你会推荐使用 i 和 range 来循环吗?
  • 实际上不可能知道您应该做什么,但是您的循环无法工作,因为files = glob.glob(path) 返回一个空列表。你的第一个问题是之前你的for循环,所以在你进入循环之前需要修复。

标签: python arrays loops numpy glob


【解决方案1】:

您的问题可能是您如何访问目录中的文件。看到这个: Use a Glob() to find files recursively in Python?

【讨论】:

  • 嗨尼克,我也试过了(但也没用): directory = os.path.join("/textanalysisfiles","path") for root,dirs,files in os.walk(directory): for file in files:
  • 同意@roganjosh ,file是python中的保留名称,所以使用其他名称,甚至:对于file_list中的文件
猜你喜欢
  • 1970-01-01
  • 2012-04-02
  • 1970-01-01
  • 2017-08-06
  • 1970-01-01
  • 1970-01-01
  • 2016-05-11
  • 2021-03-24
  • 1970-01-01
相关资源
最近更新 更多