Python从csv文件中获取确切的单元格 [英] Python getting exact cell from csv file

查看:1023
本文介绍了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.")

我几乎键入了csv文件路径以对其进行分析并执行其他操作.

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

第一个问题是:如何从CSV文件中打印确切的单元格?我尝试了不同的方法,但似乎无法正常工作.

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

另一个问题是:我可以自动打印第一个单元格(1 A)和最后一行的第一个单元格(1099 A),而无需键入单元格位置吗?

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?

谢谢

数据的小部分

数据示例:

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

推荐答案

您在最顶部导入了csv,但随后决定不使用它.我想知道为什么–似乎您只是在这里需要什么.因此,在简短浏览官方文档后,我得到了:

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)

完成.普通的pop()会删除最后一个项目,但您也可以使用索引.另外,您可以使用更多的pythonic(因为切片")data = data[1:],但是我认为这可能涉及复制和移动大量数据.

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)')

看,内存中有数据!由于以下原因,我将其粘贴在(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]}"')

其中显示

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]

然后您得到

First row, last cell contains "0"

,现在您可以使用data[row][column]寻址所需的任何单元(当然,仅在有效范围内).

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

免责声明:这是我第一次查看csv模块.某些操作可能会更有效地完成.实际上,官方文档中的所有示例 verbatim ,事实证明,首先在这里首先看看是值得的.

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.

这篇关于Python从csv文件中获取确切的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆