在Python中处理目录内容时使用For循环

问题描述 投票:0回答:2

我试图遍历目录中的一系列文本文件,查找某些类型的单词的出现,并为每个找到的单词加上用户定义的标签前缀。我的代码如下。

ACC_Tagged_Test = 'C:/ACC_Tag_Test'

for filename in glob.glob(os.path.join(ACC_Tagged_Test, '*.txt')):
 with open(filename) as f:
    data = f.read()
    data = data.lower()

modals = {"could":1, "would":1, "should":1, "can":1, "may":1, "might":1}
personal_attribute = {"believes":1, "guess":1, "surmise":1, "considers":1, 
"presume":1, "speculate":1, "postulate":1, "surmised":1, "assume":1}
approx_adapt = {"broadly":1, "mainly":1, "mostly":1, "loosely":1, 
"generally":1, "usually":1,"typically":1, "regularly":1, "widely":1}
plaus_shields = {"wonder":1, "suspect":1, "theorize":1, "hypothesize":1, 
"cogitate":1, "contemplate":1, "deliberate":1}

format_modal = "<555>{} ".format
format_attribute = "<666>{} ".format
format_app_adaptor = "<777>{} ".format
format_plaus_shield = "<888>{} ".format


data = " ".join(format_modal(word) if word in modals else word for word in data.split())

data = " ".join(format_attribute(word) if word in personal_attribute else word for word in data.split())

data = " ".join(format_app_adaptor(word) if word in approx_adapt else word for word in data.split())

data = " ".join(format_plaus_shield(word) if word in plaus_shields else word for word in data.split())

with open (filename, "w") as f:

 f.write(str(data))
 print(data) # This is just added in order to check on screen all files
              # Are being processed.

我的问题是,尽管代码对目录中的最后一个文件起作用,但对先前的文件不起作用(在此文件中占10个),但我已经在文件写出语句上方尝试了第二个For循环,但这不是都在工作。谁能在这里解释我做错了什么?

问候

python for-loop tagging
2个回答
0
投票

假定所有代码都应该在for循环中。您正在覆盖您的文本文件,因此看起来只有您的上一次运行有效:

#this overrides the file
with open(filename, "w") as fh:
    fh.write(str(data))

更改为:

#this append to the file
with open(filename, "a") as fh:
    fh.write(str(data))

这将追加到您的文本文件,并且不会使用来自上一个循环的数据覆盖先前添加的数据。


0
投票

我推测您的代码仅显示最后一个文件,因为它是没有正确缩进以使所有相关代码都在for循环内。

尝试使用此缩进:

ACC_Tagged_Test = 'C:/ACC_Tag_Test'

for filename in glob.glob(os.path.join(ACC_Tagged_Test, '*.txt')):
  with open(filename) as f:
      data = f.read()
      data = data.lower()

  modals = {"could":1, "would":1, "should":1, "can":1, "may":1, "might":1}
  personal_attribute = {"believes":1, "guess":1, "surmise":1, "considers":1, 
  "presume":1, "speculate":1, "postulate":1, "surmised":1, "assume":1}
  approx_adapt = {"broadly":1, "mainly":1, "mostly":1, "loosely":1, 
  "generally":1, "usually":1,"typically":1, "regularly":1, "widely":1}
  plaus_shields = {"wonder":1, "suspect":1, "theorize":1, "hypothesize":1, 
  "cogitate":1, "contemplate":1, "deliberate":1}

  format_modal = "<555>{} ".format
  format_attribute = "<666>{} ".format
  format_app_adaptor = "<777>{} ".format
  format_plaus_shield = "<888>{} ".format


  data = " ".join(format_modal(word) if word in modals else word for word in data.split())

  data = " ".join(format_attribute(word) if word in personal_attribute else word for word in data.split())

  data = " ".join(format_app_adaptor(word) if word in approx_adapt else word for word in data.split())

  data = " ".join(format_plaus_shield(word) if word in plaus_shields else word for word in data.split())

  with open (filename, "w") as f:
    f.write(str(data))
    print(data) # This is just added in order to check on screen all files
                # Are being processed.
© www.soinside.com 2019 - 2024. All rights reserved.