Hello I have a csv file that contains those columns :
index, text , author , date
i want to select the last column from the last inserted row
what i did so far :
inputFile = 'bouyguesForum_results.csv'
f1 = open(inputFile, "r")
last_line = f1.readlines()[-1]
f1.close()
print (last_line)
this code gets me the last inserted row but i want to select the last column which is the date column
code output :
9,"J'ai souscrit à un abonnement Bbox de 6€99 3€ de location de box, sauf que j'ai été prélevé de 19€99 ce mois-ci, sachant que je n'ai eu aucune consommation supplémentaire, ni d'appel, et je n'ai souscrit à rien, et rien n'est précisé sur ma facture. Ce n'est pas normal, et je veux une explication.",JUSTINE,17 novembre 2021
thank you for your time.
CodePudding user response:
You can do this: if you want the very last row
with open('data.csv', 'r') as csv:
data = [[x.strip() for x in line.strip().split(',')] for line in csv.readlines()][-1][-1]
print(data)
or if you want all the last elements in each row
with open('data.csv', 'r') as csv:
data = [line.strip().split(',')[-1] for line in csv.readlines()]
print(data)
CodePudding user response:
Since you got the last row, now you can just split it into a list. Sample-
last_line = last_line.strip("\n")
last_line = [x for x in last_line.split(",") if x!=""]
last_date = last_line[-1]