简体   繁体   English

从Excel和CSV文件中读取特定的行/列

[英]Reading from a specific row/column from and excel csv file

I am a beginner at Python and I'm looking to take 3 specific columns starting at a certain row from a .csv spreadsheet and then import each into python. 我是Python的初学者,我希望从.csv电子表格的某行开始提取3个特定列,然后将每个列导入python。

For example 例如

I would need to take 1000 rows worth of data from column F starting at row 12. 从第12行开始,我将需要从F列中获取1000行的数据。

I've looked at options using cvs and pandas but I can't figure out how to have them start importing at a certain row/column. 我已经看过使用cvs和pandas的选项,但是我不知道如何让它们在特定的行/列开始导入。

Any help would be greatly appreciated. 任何帮助将不胜感激。

If the spreadsheet is not huge, the easiest approach is to load the entire CSV file into Python using the csv module and then extract the required rows and columns. 如果电子表格不是很大,最简单的方法是使用csv模块将整个CSV文件加载到Python中,然后提取所需的行和列。 For example: 例如:

import csv
rows = list(csv.reader(file('Book1.csv', 'rb')))
data = [column[5] for column in rows[11:11+1000]]

will do the trick. 会成功的 Remember that Python starts numbering from 0, so column[5] is column F from your spreadsheet and rows[11] is row 12. 请记住,Python从0开始编号,因此column[5]是电子表格中的F列,而rows[11]是第12行。

CSV files being text files, there is no way to read a certain line. CSV文件是文本文件,无法读取特定行。 You will have to read line per line, and count... Have a look at the csv module in Python, which will explain how to (easily) read lines. 您将不得不每行读取一行,然后计数...看一下Python中的csv模块 ,它将解释如何(轻松)读取行。 Particularly this sectio n. 特别是这个部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM