这里有一个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 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..。没有错误,但它对csv文件没有任何影响。有什么想法吗?也在print "Forecast Complete! Please check the file!" ..。当我运行程序,它没有到达那里,这意味着一定有什么问题的循环?我现在正在想办法。但我也想寻求帮助。
原始档案:
1 2 3
1 2 3
1 2 3我想要的是:
1 2 3 result(digits)
1 2 3 result(digits)
1 2 3 result(digits)发布于 2017-04-19 10:39:23
简短回答
pyexcel.iget_records返回一个字典列表,适用于具有标题行的数据。'records.save_as‘不能工作,因为返回的数据结构是一个标准的Python,它自然没有save_as函数。
pyexcel.Sheet实例将有一个作为函数,但是应该在代码中使用“pyexcel.get_sheet”。或者作为,模块级函数可以将数组保存到文件中。见这里的例子。
样品溶液
>>> 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')长答案
pyexcel帮助您在一个队列中获得python结构。有4项职能:get_array,get_dict,get_records和get_book_dict。提供了用于处理更大数据大小的iget_records.和iget_array。假设开发人员只需要数据进行分析。
pyexcel提供两个自定义函数来帮助您操作数据:、get_sheet、和get_book。前者返回pyexcel.Sheet,后者返回pyexcel.Book。假设开发人员需要操作行、列和工作表,然后将工作表/书籍保存回excel文件。
然而,尽管如此,通用python数据结构可以很容易地作为excel文件存储。下面是模块级的三存函数:save_as和save_book_as。下面是如何将数组保存到xls文件中上的一个示例。
与熊猫相比,pyexcel是一个重量轻、安装方便、易于理解和基于组件的excel数据处理包。然而,它的目的并不是取代熊猫,而是为不那么复杂的数据分析任务提供一个替代方案。
与openpyxl和xlsxwriter相比,pyexcel允许您专注于数据,而不是文件格式,在这里您可以重用代码来处理ods、xls和csv文件格式,而无需更改代码。
https://stackoverflow.com/questions/43396437
复制相似问题