Python 使用Pyexcel处理Excel数据
这里有一个Python问题: 您好,我正在制作一个web应用程序,它从一个电子表格(.csv)接收数据,并从中将其转换为整数。计算这些值,返回这些值,并将数据写入表中每行的第4列。正如您在我的代码中所看到的:Python 使用Pyexcel处理Excel数据,python,excel,csv,Python,Excel,Csv,这里有一个Python问题: 您好,我正在制作一个web应用程序,它从一个电子表格(.csv)接收数据,并从中将其转换为整数。计算这些值,返回这些值,并将数据写入表中每行的第4列。正如您在我的代码中所看到的: import fileinput import csv import pyexcel as pe records = pe.iget_records(file_name="test.xlxs") cho = raw_input("\nStart Forecaster on fi
import fileinput
import csv
import pyexcel as pe
records = pe.iget_records(file_name="test.xlxs")
cho = raw_input("\nStart Forecaster on file?:<1/0>")
if cho == 1:
for record in records:
rem = record[i,0]
sold1 = record[i,1]
sold2 = record[i,2]
rem = int(rem)
sold1 = int(sold1)
sold2 = int(sold2)
result = forecast(rem,sold1,sold2)
record[i,4] = result
print "Forecast Complete! Please check the file!"
else:
quit()
def calculate(rem,sold1,sold2):
result = ((l+t)/2)*3
return result
def forecast(rem,sold1,sold2):
if (rmn == 0 and sold1 == 0 and sold2 ==0): #All ZERO
return 15
elif (rmn == 0 and sold1 == 0 and sold2 < 10): #ALL FOR ONE PRODUCT VALUE
return sold2*3
elif (rmn == 0 and sold1 < 10 and sold2 ==0):
return sold1*3
elif (rmn < 10 and sold1 == 0 and sold2 == 0):
return rmn*3
#END FOR ONE PRODUCT VALUE
elif (rmn>= 10 and sold1>=10 and sold2>=10):
if((rmn/3)>=(sold1+10) or (rmn/3)>=(sold1+10)):
return 0
else:
return calculate(rmn,sold1,sold2)-rmn
elif (rmn<10 and sold1<10 and sold2<10):
return calculate(rmn,sold1,sold2)
elif (rmn == 0 and sold1>=10 and sold2>=10):
return calculate(rmn,sold1,sold2)
else:
return sold1
我想发生的事:
1 2 3 result(digits)
1 2 3 result(digits)
1 2 3 result(digits)
简短回答
pyexcel.iget_记录返回字典列表,适用于具有标题行的数据records.save_as'将不起作用,因为返回的数据结构是一个标准的Python列表,它自然没有save_as函数
pyexcel.Sheet实例将有一个函数,但代码中应使用“pyexcel.get_Sheet”。或者,模块级函数可以将数组保存到文件中。看
样品溶液
>>将pyexcel作为p导入
>>>sheet=p.get_sheet(文件_name='test.xlsx')#pip安装pyexcel xlsx
>>>床单
第1页:
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
>>>对于图纸中的行:
... 打印行
...
[1, 2, 3]
[1, 2, 3]
[1, 2, 3]
>>>结果=[]
>>>对于图纸中的行:
... 结果。追加(总和(行))#>>结果
[6, 6, 6]
>>>sheet.column+=结果
>>>床单
第1页:
+---+---+---+---+
| 1 | 2 | 3 | 6 |
+---+---+---+---+
| 1 | 2 | 3 | 6 |
+---+---+---+---+
| 1 | 2 | 3 | 6 |
+---+---+---+---+
>>>sheet.save_as('new.csv')
长话短说
pyexcel帮助您在一行程序中获得python数据结构。有:get_array、get_dict、get_records和get_book_dict。用于处理较大的数据量:iget_array和iget_records。假设开发人员只需要用于分析的数据
pyexcel提供了帮助您操作数据的功能:获取工作表和获取书籍。前者返回,后者返回。假设开发人员需要操作行、列和图纸,然后将图纸/书籍保存回excel文件
尽管如此,通用python数据结构可以轻松地存储为excel文件。以下是模块级:另存为和另存为。下面是一个关于的例子
与pandas相比,pyexcel是一个重量轻、易于安装、易于理解、基于组件的excel数据处理软件包。然而,它的目的不是取代熊猫,而是提供一种替代数据分析任务的方法,该任务不太复杂
与openpyxl和xlsxwriter相比,pyexcel让您可以专注于数据,而不是文件格式,您可以重用代码来处理ods、xls和csv文件格式,而无需更改代码我没有专门使用该库,但看起来您并没有在任何地方向文件写入任何内容
records=pe.iget\u records(file_name=“test.xlxs”)
只是将文件内容拉入Python,在Python中对其进行操作,然后将结果扔掉。请看一下关于写入/保存更改的说明。我尝试了记录。另存为(“test.xlsx”)
但仍然不起作用,您如何看待我的循环?有什么问题吗?我现在没有时间玩这个库,也没有测试数据。但是,在保存数据之前,您应该使用打印的来验证您是否实际计算了您认为自己拥有的数据。我可以推荐pandas
、openpyxl
、或xlsxwriter
模块,而不是pyexcel
?chfw的另一个传奇答案:)
1 2 3 result(digits)
1 2 3 result(digits)
1 2 3 result(digits)
>>> import pyexcel as p
>>> sheet = p.get_sheet(file_name='test.xlsx') # pip install pyexcel-xlsx
>>> sheet
Sheet 1:
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
>>> for row in sheet:
... print row
...
[1, 2, 3]
[1, 2, 3]
[1, 2, 3]
>>> results = []
>>> for row in sheet:
... results.append(sum(row)) # <- do your own forcast here
...
>>> results
[6, 6, 6]
>>> sheet.column += results
>>> sheet
Sheet 1:
+---+---+---+---+
| 1 | 2 | 3 | 6 |
+---+---+---+---+
| 1 | 2 | 3 | 6 |
+---+---+---+---+
| 1 | 2 | 3 | 6 |
+---+---+---+---+
>>> sheet.save_as('new.csv')