python 读csv单元格,Python从csv文件获取确切的单元格

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.

8f8ac93af1e9d192199a11958659f5ca.png

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

解决方案

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值