Python – 以CSV格式从Excel电子表格打印单个单元格

我有一个Excel电子表格保存为CSV文件,但无法find一种方法,使用CSV模块将单元格中的单个值调用到Python中。 任何帮助将不胜感激

还有一个Python库能够读取xls数据。 看看python-xlrd

为了编写xls数据,你可以使用python-xlwt

csv模块提供遍历csv文件行的读取器 – 是string列表。 访问单个单元格的一种方法是:

阅读列表中的整个文件

 import csv with open('test.csv', 'r') as f: reader = csv.reader(f) the_whole_file = list(reader) 

然后通过索引到the_whole_file访问单个单元。 第一个索引是行,第二个索引是列 – 都是基于零的。 要访问第二行第四列的​​单元格,请执行以下操作:

 row = 1 column = 3 cell_R1_C3 = the_whole_file[row][column] print cell_R1_C3 

如果您将excel文件作为CSV文件,则可以使用csv.reader

 import csv myFilePath = "/Path/To/Your/File" with open(myFilePath,'rb') as csvfile: reader = csv.reader( csvfile, delimiter=',' ) for row in reader: # 'row' has all the cells (thanks to wwii for the fix!). Get the first 4 columns a, b, c, d = row[:4]