简体   繁体   English

使用 python 从 csv 文件中获取最后一列的最后一行

[英]Get last row in last column from a csv file using python

Hello I have a csv file that contains those columns:您好,我有一个包含这些列的 csv 文件:

index, text , author , date 

i want to select the last column from the last inserted row我想 select 最后插入的行的最后一列

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这段代码让我得到最后插入的行,但我想 select 最后一列是日期列

code output:代码 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.感谢您的时间。

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)

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM