Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 read, and it has", size, "lines.", size - 1, "rows has been analyzed.")

I pretty much type the csv file path to analyze and do different things with it.

First question is: How can I print the exact cell from the CSV file? I have tried different methods, but I can't seem to get it working. For example I want to print the info of those two cells

For example I want to print the info of those two cells

The other question is: Can I automate it to print the very first cell(1 A) and the very last row first cell (1099 A), without me needing to type the cell locations?

Thank you

Small portion of data

Example of the data:

Time    Solar Carport   Solar Fixed  SolarFlatroof  Solar Single
1.1.2016    317         1715         6548           2131
2.1.2016    6443        1223         1213           23121
3.1.2016    0           12213        0              122
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
157 views
Welcome To Ask or Share your Answers For Others

1 Answer

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 ('	'.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

First row, first cell contains "2016-01-01 00:00:00"
First row, 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

First row, 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).

Disclaimer: this is my very first look at the csv module. Some operations could possibly be done more efficiently. Practically all examples verbatim from the official documentation, which proves it's always worth taking a look there first.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...