1

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.

4
  • 2
    Are you confusing rows with columns? Do you want to extract the date column? Commented Nov 19, 2021 at 11:04
  • Provide sample test data and the output you want. Commented Nov 19, 2021 at 11:06
  • oh yes i was confusing rows with columns sorry also i provided a code output in post Commented Nov 19, 2021 at 11:12
  • Please do not edit solution announcements into the question. Accept (i.e. click the "tick" next to it) one of the existing answer, if there are any. You can also create your own answer, and even accept it, if your solution is not yet covered by an existing answer. Compare stackoverflow.com/help/self-answer Commented Sep 12, 2022 at 10:29

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.