简体   繁体   English

使用 DictReader 在 Python 中获取 CSV 文件的最后一行

[英]Get Last Line Of CSV File In Python Using DictReader

Didn't manage to find any answer for that, I prefer to use DictReader and still be able to reach last line of CSV File efficiently, any thoughts?没有找到任何答案,我更喜欢使用 DictReader 并且仍然能够有效地到达 CSV 文件的最后一行,有什么想法吗?

if you keep the values in a list you can try "-1"如果您将值保留在列表中,您可以尝试“-1”

list[-1]

Just iterate the file... The last line will be in the loop variable:只需迭代文件...最后一行将在循环变量中:

import csv

with open("test.csv") as f:
    reader = csv.DictReader(f)
    for line in reader:
        pass

print(line)

This piece of code will do the required thing for you efficiently这段代码将有效地为您完成所需的事情

with open("test.csv", "r") as f:
    last_line = f.readlines()[-1]
    print(last_line)

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

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