简体   繁体   English

在python中使用readline()读取txt文件,但在aws comprehend api中未读取第二行

[英]using readline() in python to read a txt file but the second line is not read in aws comprehend api

I am reading a text file and passing it to the API, but then I am getting the result only for the first line in the file, the subsequent lines are not being read. 我正在读取文本文件并将其传递给API,但是随后我仅获得文件中第一行的结果,而未读取后续行。

code below : 下面的代码:

filename = 'c:\myfile.txt'
with open(filename) as f:
    plain_text = f.readline()  

    response = client_comprehend.detect_entities(
        Text=plain_text,
        LanguageCode='en'
    )
    entites = list(set([x['Type'] for x in response['Entities']]))

    print response
    print entites

When you are doing with f.readline() it will only take the first line of the file. 使用f.readline() ,它将仅占用文件的第一行。 So if you want to go through each line of the file you have to loop through it. 因此,如果要遍历文件的每一行,则必须遍历文件。 Otherwise if you want to read the entire file(not meant for big files) you can use f.read() 否则,如果您想读取整个文件(不适用于大文件),则可以使用f.read()

filename = 'c:\myfile.txt'
with open(filename) as f:
    for plain_text in f:
         response = client_comprehend.detect_entities(
             Text=plain_text,
             LanguageCode='en'
         )
         entites = list(set([x['Type'] for x in response['Entities']]))

         print response
         print entites

As csblo has pointed out in the comments, your readline is only reading the first line of the file because it's only being called once. 正如csblo在注释中指出的那样,您的readline仅读取文件的第一行,因为它仅被调用一次。 readline is called once in your program as it is written, it performs the actions for the single line that has been read, and then the program closes without doing anything else. readline在编写时在程序中被调用一次,它对已读取的单行执行操作,然后程序关闭而无需执行其他任何操作。

Conveniently, file objects can be iterated over in a for loop like you would a list. 方便地, 可以像在列表中那样在for循环中迭代文件对象。 Iterating over a file will return one line per iteration, as though you had called readline and assigned it to a value. 对文件进行迭代将在每次迭代中返回一行,就像您已调用readline并将其分配给一个值一样。 Using this, your code will work when rewritten as such: 使用此方法,您的代码将按以下方式工作:

filename = 'c:\myfile.txt'
with open(filename) as f:
    for plain_text_line in f:
        response = client_comprehend.detect_entities(
            Text=plain_text_line,
            LanguageCode='en'
        )
        entites = list(set([x['Type'] for x in response['Entities']]))

        print response
        print entites

This should iterate over all lines of the file in turn. 这应该依次遍历文件的所有行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM