Python – Use multiple str.startswith() in a for loop get their specific values
Question:
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
Answers:
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
As I said in my comment, you are consuming k
as you go.
To see what is going on, run the following:
file_name = "./abc.csv"
file_list=[]
with open(file_name) as k:
firstline = k.readline() # Consume first line
if firstline.startswith('TrakPro'):
print("First line:n", firstline)
file_list.append(file_name)
txt = list(k) # here k is the file contents minus the first line
print("Contents of listn", txt)
if str(txt[2]).startswith('Serial'):
file_list.append(str(txt[2])[15:26])
if str(txt[3]).startswith('Test'):
file_list.append(str(txt[3].strip())[11:])
print("nResult:n",file_list)
Where the contents of abc.csv are:
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:,
The result should be:
First line:
TrakPro Version 5.2.0.0 ASCII Data File
Contents of list
['Instrument Name:,SidePakn', 'Model Number:,TK0W02n', 'Serial Number:,120k2136005n', 'Test Name:,13270n', 'Start Date:,04/17/2021n', 'Start Time:,01:53:29n', 'Duration (dd:hh:mm:ss):,00:07:13:00n', 'Log Interval (mm:ss):,01:00n', 'Number of points:,433n', 'Description:,n']
Result:
['./abc.csv', '120k2136005', '13270']
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
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
As I said in my comment, you are consuming k
as you go.
To see what is going on, run the following:
file_name = "./abc.csv"
file_list=[]
with open(file_name) as k:
firstline = k.readline() # Consume first line
if firstline.startswith('TrakPro'):
print("First line:n", firstline)
file_list.append(file_name)
txt = list(k) # here k is the file contents minus the first line
print("Contents of listn", txt)
if str(txt[2]).startswith('Serial'):
file_list.append(str(txt[2])[15:26])
if str(txt[3]).startswith('Test'):
file_list.append(str(txt[3].strip())[11:])
print("nResult:n",file_list)
Where the contents of abc.csv are:
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:,
The result should be:
First line:
TrakPro Version 5.2.0.0 ASCII Data File
Contents of list
['Instrument Name:,SidePakn', 'Model Number:,TK0W02n', 'Serial Number:,120k2136005n', 'Test Name:,13270n', 'Start Date:,04/17/2021n', 'Start Time:,01:53:29n', 'Duration (dd:hh:mm:ss):,00:07:13:00n', 'Log Interval (mm:ss):,01:00n', 'Number of points:,433n', 'Description:,n']
Result:
['./abc.csv', '120k2136005', '13270']