Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不知道用python excel读取数据文件_Python_Excel - Fatal编程技术网

不知道用python excel读取数据文件

不知道用python excel读取数据文件,python,excel,Python,Excel,我是一名学生,没有时间做这项工作。 所以问题是下一个。 我有一部分代码: import matplotlib.pyplot as plt from pylab import * import cmath def sf(prompt): """ """ error_message = "Value must be integer and greater or equal than zero" while True: val = raw_input(prompt)

我是一名学生,没有时间做这项工作。 所以问题是下一个。 我有一部分代码:

import matplotlib.pyplot as plt
from pylab import *
import cmath
def sf(prompt):
""" """
    error_message = "Value must be integer and greater or equal than zero"
    while True:
        val = raw_input(prompt)
        try:
            val = float(val)
        except ValueError:
            print(error_message)
            continue
        if val <= 0:
            print(error_message)
            continue
        return val
def petrogen_elements():
    """Input and calculations the main parameters for
    pertogen elements"""
    print "Please enter Petrogen elements: \r"
    SiO2 = sf("SiO2: ")
    Al2O3= sf("Al2O3: ")
    Na2O = sf("Na2O: ")
    K2O =  sf("K2O: ")

    petro = [SiO2,TiO2,Al2O3,]                    

    Sum = sum(petro)

    Alcal = Na2O + K2O
    TypeA lcal= Na2O / K2O
    Ka= (Na2O + K2O)/ Al2O3 

    print  '-'*20, "\r Alcal: %s \r TypeAlcal: %s  \
     \r Ka: %s \r" % (Alcal, TypeAlcal,Ka,)

petrogen_elements()
所有excel文件只有5列和无限行。 用户可以选择输入数据或导入excel文件。 我已经完成了工作的第一部分,但它仍然是一个重要部分 最后,我需要读取所有文件并计算值

如果您能给我一些建议,我将不胜感激。这个网站列出了所有与Python excel相关的主要库。 我个人试过XLRD——第一个列出的——发现它很棒,而且它有一个整洁的文档

在埃及总统选举期间,我也使用它做了一些工作,因为excel表格中有大量数据,我们需要导入mysql数据库。我已经在Github上发布了代码:

首先安装xlrd

您将提出的脚本应该类似于:

from xlrd import *
## Opening the excel file
book = open_workbook('excel_file.xls')

## Reading the sheet we need
## Most probably the data will be on the first sheet, 
## otherwise this needs to be updated
our_sheet = book.sheet_by_index(0)

## Get the rows number
rowcount = our_sheet.nrows

## Looping over sheet rows
for i in range(rowcount -1):
    ## Get the data in the row
    our_row = our_sheet.row_slice(i+1)

    ## Access each row by index and do whatever you like with it
    ## Since you have 5 columns, the index will range from 0 - 4
    print our_row[0]
    print our_row[1]
    print our_row[2]
    print our_row[3]
    print our_row[4]

您可以从我在这个文件中提到的脚本中找到一个工作示例:

如果您可以将excel文件转换为csv,您可以使用内置模块。非常感谢)这真的很有帮助)
from xlrd import *
## Opening the excel file
book = open_workbook('excel_file.xls')

## Reading the sheet we need
## Most probably the data will be on the first sheet, 
## otherwise this needs to be updated
our_sheet = book.sheet_by_index(0)

## Get the rows number
rowcount = our_sheet.nrows

## Looping over sheet rows
for i in range(rowcount -1):
    ## Get the data in the row
    our_row = our_sheet.row_slice(i+1)

    ## Access each row by index and do whatever you like with it
    ## Since you have 5 columns, the index will range from 0 - 4
    print our_row[0]
    print our_row[1]
    print our_row[2]
    print our_row[3]
    print our_row[4]