0

How to read last.specific line and column of CSV file with python

Hi!... Actually I can use re.findall to search a specific pattern, but now I need to retrieve the value in a specific column of that line. I'm not sure how well am I doing that, but here is a try I did according to some stuffs I've read in Sololearn ######## import re pattern="19:30" #Actually I can fin the line that contains the hour I need to extract #But I preffer some method to get the latest line of the file def count_char(text, char): count = 0 for c in text: if c == char: count += 1 return count filename = input("Enter a filename: ") with open(filename) as f: text = f.read() alfa=len((re.findall(pattern, text))) print(alfa) #Print how many times my patter appears... just one time in this case f.close() #Close file

13th Aug 2017, 5:31 AM
William Jimenez Urda
William Jimenez Urda - avatar
2 Answers
+ 1
if read you correctly you should try the following: while (f): line= f.readline() # read() - for whole file data_per_column = line.split(",") # if CSV file if data_per_column [col_number] == what_needed: break
10th Sep 2017, 8:39 AM
yuri
0
thanks Yuri. I do really appreciate your wonderful concise help. thank you so much
15th Sep 2017, 4:24 AM
William Jimenez Urda
William Jimenez Urda - avatar