Reputation: 3025
I am receiving an XML response from an API in a variable r so that:
print r.read()
displays the entire XML response.
I need to take the first line of this response and convert it to a string variable for use in a loop after some substringing. What is the best way to capture just the first line?
Upvotes: 2
Views: 1431
Reputation: 52788
r.readline()
Built-in Types - File Objects:
file.readline([size])
Read one entire line from the file. A trailing newline character is kept in the string (but may be absent when a file ends with an incomplete line). If thesize
argument is present and non-negative, it is a maximum byte count (including the trailing newline) and an incomplete line may be returned. Whensize
is not 0, an empty string is returned only when EOF is encountered immediately.r.readline() 'This is the first line of the file.\n' r.readline() 'Second line of the file\n' r.readline() ''
Upvotes: 4