Skip to content
Advertisement

Python – Use multiple str.startswith() in a for loop get their specific values

The below function parses multiple csv files in a directory and takes out values using str.startwith().

It works find using ‘firstline.startswith(‘TrakPro’)‘ and ‘txt.startswith('Serial')‘. However, when I add a third str.startwith() i.e. txt2.startswith(‘Test’), nothing prints out, no error, appears to ignore it. What do I need to change? Basically I want to add multiple str.startwith() in the for loop pulling out various key words after the ‘:”.

JavaScript

The csv looks like this:

JavaScript

So far I have tried the above code, I expected to print out values in the ‘Test Name’ line of the csv sample. The function does not print out anything, no error.

Tks

Advertisement

Answer

To print only the value of the line that starts with Test Name: you can use following code:

JavaScript

Prints:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement