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 ‘:”.

def get_csv_file_list(root):
    for r, d, f in os.walk(root):
        for file in f:

            if file.endswith('.csv'):
                path = os.path.join(r, file)
                dir_name = path.split(os.path.sep)[-2]
                file_name = os.path.basename(file)

                try:
                    with open(path) as k:
                        firstline = k.readline()

                        if firstline.startswith('TrakPro'):
                            file_list.append(path)
                            file_list.append(dir_name)
                            file_list.append(file_name)

                            txt = 'Serial Number:'
                            if txt.startswith('Serial'):
                                for row in list(k)[3:4]:
                                    file_list.append(row[15:26])

                            txt2 = 'Test Name:'
                            if txt2.startswith('Test'):
                                for rows in list(k)[4:5]:
                                    print(rows)
                                    file_list.append(row[11:])

The csv looks like this:

TrakPro Version 5.2.0.0 ASCII Data File
Instrument Name:,SidePak
Model Number:,TK0W02
Serial Number:,120k2136005
Test Name:,13270
Start Date:,04/17/2021
Start Time:,01:53:29
Duration (dd:hh:mm:ss):,00:07:13:00
Log Interval (mm:ss):,01:00
Number of points:,433
Description:,

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:

with open("your_file.csv", "r") as f_in:
    for line in map(str.strip, f_in):
        if line.startswith("Test Name:"):
            _, value = line.split(",", maxsplit=1)
            print(value)

Prints:

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