简体   繁体   中英

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.

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. 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()

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. 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.

Conveniently, file objects can be iterated over in a for loop like you would a list. Iterating over a file will return one line per iteration, as though you had called readline and assigned it to a value. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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