从python文件中读取

时间:2015-12-03 14:08:35

标签: python

我有一个文本文件,其格式为:

  

能够||无法
  无法||能够
  远轴||正面
  adaxial ||背面

我需要检查这个词是否是另一个词的反义词。

我所做的是这样的代码:

def antonyms():
    f = open('antonyms.txt', 'r+')
    for line in f:
        a = line.split('||')
        x = a[0]
        y = a[1]
    return x, y

但我得到的只是最后一对,然后我试着缩进返回一点所以我做了

def antonyms():
    f = open('antonym_adjectives.txt', 'r+')
    for line in f:
        a = line.split('||')
        x = a[0]
        y = a[1]
        return x, y

但我再次只有第一对

我如何获得所有配对?

我该怎么做:

>>> antonyms(x, y)

告诉我他们是对还是错?

6 个答案:

答案 0 :(得分:1)

你的问题是return你正在退出这个职能部门。

而是x = a[0]y = a[1],将这些值附加到数组中,然后返回该数组。

the_array = []
for line in f:
    a = line.split('||')
    the_array.append((a[0],a[1]))
return the_array

答案 1 :(得分:1)

你可以使用yield:

def antonyms():
    f = open('antonyms.txt', 'r+')
    for line in f:
        a = line.split('||')
        x = a[0]
        y = a[1]
        yield x, y

for a,b in antonyms():
     # rest of code here

顺便说一下,你可以直接分配给x和y:

x,y = line.split('||')

简单检查a和b是反义词是否可以:

(a == 'un' + b) or (b == 'un' + a)

答案 2 :(得分:1)

编写一个获取给定单词的反义词的函数会更有意义:

def antonym(word):
    with open('antonym_adjectives.txt', 'r+') as f:
        for line in f:
            a, b = line.split('||')
            a = a.strip()
            b = b.strip()
            if a == word:
                return b
            if b == word:
                return a

然后,您可以撰写antonym(x) == y来检查xy是否为反义词。 (但是这假设每个单词都有一个唯一的反义词)。

每次从头开始读取文件。如果您的反义词列表的大小可管理,那么将它作为数组或字典读入内存可能更有意义。

如果您不能假设每个单词都有一个唯一的反义词,您可以将其转换为一个生成器,它将返回给定单词的所有反义词。

def antonym(word):
    with open('antonym_adjectives.txt', 'r+') as f:
        for line in f:
            a, b = line.split('||')
            a = a.strip()
            b = b.strip()
            if a == word:
                yield b
            if b == word:
                yield a

然后y in antonyms(x)会告诉您xy是否为反义词,list(antonyms(x))会为您提供x的所有反义词列表。< / p>

答案 3 :(得分:1)

因为没有答案可以回答你的问题:

首先:如果你回来,程序将停在那里。所以你想要yield你的反义词,这样整个函数就变成了一个生成器:

def antonyms():
    f = open('antonyms.txt', 'r+')
    for line in f:
        a = line.split(' || ') # Think you also need surrounding spaces here.
        x = a[0]
        y = a[1]
        yield x, y

使用此功能检查is_antonym(a,b)

def is_antonym(a,b):
    for x,y in antonyms():
        if a == x and b == y:
            return True
    return False

其他答案也有很好的提示:
例如,一个很好的替代品是:[x,y] = line.split(' || ')

答案 4 :(得分:0)

这是我的代码..

def antonyms(first,second):
    f = open('text.txt', 'r')

    for line in f.readlines():
        lst = [s.strip() for s in line.split('||')]
        if lst and len(lst) == 2:
            x = lst[0]
            y = lst[1]
            if first == x and second == y:
                return True
    return False

答案 5 :(得分:0)

我认为列表不是解决此问题的良好数据结构。在将所有反义词对读入列表后,仍然需要搜索整个列表以查找单词的反义词。 dict会更有效率。

antonym = {}
with open('antonym_adjectives.txt') as infile:
    for line in infile:
       x,y = line.split('||)
       antonym[x] = y
       antonym[y] = x

现在你可以在字典中查找反义词:

try:
   opposite = antonym[word]
except KeyError:
   print("%s not found" %word)