简体   繁体   English

将 Excel 文件作为列表导入 Python

[英]Import Excel file in to Python as a list

I want to import one column with 10 rows in to Python as a list.我想将一列 10 行作为列表导入到 Python 中。

So I have in excel for example: One, Two, Three, Four,..., Ten Everything written in column A over row 1-10.所以我在 excel 中举例:一、二、三、四、...、十 所有写在 A 列中的第 1-10 行。

Now I want to import these cells into Python, so that my result is:现在我想将这些单元格导入 Python,这样我的结果是:

list = ['One', 'Two', 'Three', 'Four', ..., 'Ten']

Since I am a total noob in programming, I have no clue how to do it.由于我在编程方面完全是个菜鸟,所以我不知道该怎么做。 So please tell me the most easiest way.所以请告诉我最简单的方法。 All tutorials I have found, did't got me the result I want.我找到的所有教程都没有得到我想要的结果。 Thank you谢谢

I am using Python 2.7我正在使用 Python 2.7

Even though pandas is a great library, for your simple task you can just use xlrd :尽管pandas是一个很棒的库,但对于您的简单任务,您只需使用xlrd

import xlrd

wb = xlrd.open_workbook(path_to_my_workbook)
ws = wb.sheet_by_index(0)
mylist = ws.col_values(0)

Note that list is not a good name for a variable in Python, because that is the name of a built-in function.请注意, list不是 Python 中变量的好名称,因为它是内置函数的名称。

I am unsure if your data is in xlsx form or CSV form.我不确定您的数据是 xlsx 格式还是 CSV 格式。 If XLSX, use this Python Excel tutorial .如果是 XLSX,请使用此 Python Excel 教程 If CSV, it is much easier, and you can follow the code snippet below.如果是 CSV,则容易得多,您可以按照下面的代码片段进行操作。 If you don't want to use pandas, you can use the numpy library.如果不想使用熊猫,可以使用numpy库。 Use the example code snippet below for taking the top row of a CSV file:使用下面的示例代码片段获取 CSV 文件的第一行:

import numpy as np
csv_file = np.genfromtxt('filepath/relative/to/your/script.csv', 
                          delimiter=',', dtype=str)
top_row = csv_file[:].tolist()

This will work for a file that has only one column of text.这适用于只有一列文本的文件。 If you have more columns, use the following snippet to just get the first column.如果您有更多列,请使用以下代码段来获取第一列。 The '0' indicates the first column. “0”表示第一列。

top_row = csv_file[:,0].tolist()

I recommend installing pandas.我建议安装熊猫。

pip install pandas

and

import pandas
df = pandas.read_excel('path/to/data.xlsx') # The options of that method are quite neat; Stores to a pandas.DataFrame object
print df.head() # show a preview of the loaded data
idx_of_column = 5-1 # in case the column of interest is the 5th in Excel
print list(df.iloc[:,idx_of_column]) # access via index
print list(df.loc[['my_row_1','my_row_2'],['my_column_1','my_column_2']]) # access certain elements via row and column names
print list(df['my_column_1']) # straight forward access via column name

(checkout pandas doc ) or (结帐熊猫文档)或

pip install xlrd

code代码

from xlrd import open_workbook
wb = open_workbook('simple.xls')
for s in wb.sheets():
  print 'Sheet:',s.name
  for row in range(s.nrows):
    values = []
    for col in range(s.ncols):
       values.append(s.cell(row,col).value)
    print ','.join(values)

(example from https://github.com/python-excel/tutorial/raw/master/python-excel.pdf ) (示例来自https://github.com/python-excel/tutorial/raw/master/python-excel.pdf

bonjour, je suis etudiant en maths et je ne suis pas du tous fort en informatique mais je dois reussir a exporter sur excel une liste que j'ai sur pyzo écrit en python 3. Pouvez vous m'aidez merci. bonjour,je suis ududiant en mathes和je ne suis pas du tous for informatique mais je dois reussir python优秀出口商python 3. pouvez vous m'aidez merci。 :) :)

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

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