Skip to content Skip to sidebar Skip to footer

Python Getting Exact Cell From Csv File

import csv filename = str(input('Give the file name: ')) file = open(filename, 'r') with file as f: size = sum(1 for _ in f) print('File', filename, 'has been

Solution 1:

You import csv at the very top but then decided not to use it. I wonder why – it seems just what you need here. So after a brief peek at the official documentation, I got this:

import csv

data = []

with open('../Downloads/htviope2016.csv') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=';')
    for row in spamreader:
        data.append (row)

print("File has been read, and it has ", len(data), " lines.")

That is all you need to read in the entire file. You don't need to – for some operations, it is sufficient to process one line at a time – but with the full data loaded and ready in memory, you can play around with it.

print (f'First row length: {len(data[0])}')

The number of cells per row. Note that this first row contains the header, and you probably don't have any use for it. Let's ditch it.

print ('Discarding 1st row NOW. Please wait.')
data.pop(0)

Done. A plain pop() removes the last item but you can also use an index. Alternatively, you could use the more pythonic (because "slicing") data = data[1:] but I assume this could involve copying and moving around large amounts of data.

print ('First 10 rows are ...')
for i in range(10):
    print ('\t'.join(data[i])+'(end)')

Look, there is data in memory! I pasted on the (end) because of the following:

print (f'First row, first cell contains "{data[0][0]}"')
print (f'First row, last cell contains "{data[0][-1]}"')

which shows

Firstrow, first cell contains "2016-01-01 00:00:00"
Firstrow, last cell contains ""

because each line ends with a ;. This empty 'cell' can trivially be removed during reading (ideally), or afterwards (as we still have it in memory):

data = [row[:-1] for row in data]

and then you get

Firstrow, last cell contains "0"

and now you can use data[row][column] to address any cell that you want (in valid ranges only, of course).

Post a Comment for "Python Getting Exact Cell From Csv File"