To create a loop with parsing in Python, you can use the while
loop or the for
loop.
Here is an example of using a for
loop to iterate over a collection of data that needs to be parsed:
main.py272 chars10 lines
In this example, the data
list contains strings that need to be parsed by splitting them based on the comma delimiter. The for
loop iterates over each item in the list and parses it using the split()
method. The parsed data is then assigned to individual variables that can be used for further processing.
Alternatively, you can also use the while
loop to read and parse data from a file line by line:
main.py312 chars14 lines
In this example, the file
object is opened in read mode and the readline()
method is used to read the file line by line until the end of the file is reached (line
returns an empty string). Each line of data is then parsed using the split()
method, and the resulting data is assigned to individual variables. Finally, the parsed data is printed to the console, and the readline()
method is called again to read the next line of data.
gistlibby LogSnag