python:'str'对象没有属性'iteritems'

时间:2013-06-03 12:49:14

标签: python string

我想做一个巨大的发现并使用python替换。

tot11.txt是一个字符串(有600000个项目),我想从文件1.txt替换此处的项目。

所以例如tot11.txt有:

'alba''raim'

1.txt看起来像这样:

'alba':'barba', 'raim':'uva'

因此我会得到'barba''uva'等等......

当我运行脚本时,我收到以下错误:

Traceback (most recent call last):
  File "sort2.py", line 12, in <module>
    txt = replace_all(my_text, dic)
  File "sort2.py", line 4, in replace_all
    for i, j in dic.iteritems():
AttributeError: 'str' object has no attribute 'iteritems'

如果我不使用文本文件,只需在脚本中编写可更改的项目,脚本也可以正常工作。

import sys

def replace_all(text, dic):
    for i, j in dic.iteritems():
        text = text.replace(i, j)
    return text

my_text= open('tot11.txt', 'r').read()

reps = open('1.txt', 'r').read()

txt = replace_all(my_text, reps)

f = open('results.txt', 'w')
sys.stdout = f
print txt

4 个答案:

答案 0 :(得分:6)

open('1.txt', 'r').read()返回不是字典的字符串。

>>> print file.read.__doc__
read([size]) -> read at most size bytes, returned as a string.

如果1.txt包含:

'alba':'barba', 'raim':'uva'

然后您可以使用ast.literal_eval获取字典:

>>> from ast import literal_eval
>>> with open("1.txt") as f:
       dic = literal_eval('{' + f.read() +'}')
       print dic
...     
{'alba': 'barba', 'raim': 'uva'}

您应该使用str.replace而不是regex,而不是str.replace('alba','barba') 还会替换'albaa''balba'等字词:

import re
def replace_all(text, dic):
    for i, j in dic.iteritems():
        text = re.sub(r"'{}'".format(i), "'{}'".format(j), text)
    return text

答案 1 :(得分:0)

replace_all函数的第二个参数是一个字符串,因为它来自reps = open('1.txt','r')。read()....所以在字符串对象上调用iteritems()失败,因为字符串对象不存在该函数。

答案 2 :(得分:0)

您不需要使用literal_eval。 这是你的档案:

% cat 1.txt 
foo:bar
abc:def

这是将其读入字典的代码。正如Ashwini Chaudhary所说,你得到了这个错误,因为阅读read()会返回一个字符串。字符串没有名为iteritems的方法。

>>> dic = {}
>>> with open('1.txt') as f:
...     for line in f:
...             trimmed_line = line.strip()
...             if trimmed_line:
...                     (key, value) = trimmed_line.split(':')
...                     dic[key]=value
... 
>>> dic
{'foo': 'bar', 'abc': 'def'}

这当然假设您的文件中每行只有1 :

答案 3 :(得分:0)

首先,您应该在某个文件中获取替换内容:

lookup = {}  # an empty dictionary
with open('replacements.txt') as f:
   for line in f:
      if ':' in line:
          bits = line.strip().split(':')
          lookup[bits[0].strip()] = bits[1].strip()

接下来,阅读您要替换的文件:

with open('somefile.txt') as infile, open('results.txt','w') as out:
    for line in infile:
       words = line.split()  # splits on whitespace
       for word in words:
           # For each word, see if it has a replacement
           # if it does, write the replacement otherwise write the word
           # to the outfile
           out.write(lookup.get(word,word))