Skip to content Skip to sidebar Skip to footer

How To Find Last Line In Csv File When Using Dictreader

I have the following code that transforms a .csv file to a .json one. Because I have multiple csv records, and I will need to operate on multiple json objects, I was thinking of cr

Solution 1:

You can convert the dict records produced by the DictReader generator into a list first before dumping it as JSON:

jsonfile.write(json.dumps(list(reader)))

Solution 2:

a=open('file.txt','rb')
lines = a.readlines()
iflines:
    first_line = lines[:1]
    last_line = lines[-1]

check this : StackOverFlow question 1StackOverFlow question 2

Post a Comment for "How To Find Last Line In Csv File When Using Dictreader"