python - 使用 Pyexcel 处理 Excel 数据

标签 python excel csv

这是一个Python问题:

您好,我正在制作一个网络应用程序,它从电子表格(.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 文件没有任何影响。有任何想法吗?另外,在打印“预测完成!请检查文件!” ..当我运行程序时,它没有到达那里,这意味着循环一定有问题?我现在正在想办法。但我也想寻求帮助。

原始文件:

1  2  3
1  2  3
1  2  3

我想要发生的事情:

1  2  3  result(digits)
1  2  3  result(digits)
1  2  3  result(digits)

最佳答案

简短回答

pyexcel.iget_records 返回字典列表,适用于带有标题行的数据。 “records.save_as”不起作用,因为返回的数据结构是一个标准的Python列表,它自然没有save_as功能。

pyexcel.Sheet 实例将具有 save_as函数,但应在代码中使用“pyexcel.get_sheet”。或pyexcel.save_as ,模块级函数可以将数组保存到文件中。请参阅the example here .

示例解决方案

>>> 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 functions :get_array、get_dict、get_records 和 get_book_dict。 Two streaming functions提供处理更大的数据大小:iget_array 和 iget_records。假设开发人员仅需要数据进行分析。

pyexcel提供two custom functions帮助您操作数据:get_sheet 和 get_book。前一个返回pyexcel.Sheet后者返回 pyexcel.Book 。假设开发人员需要操作行、列和工作表,然后将工作表/书籍保存回 Excel 文件。

话虽如此,通用的 Python 数据结构可以轻松地存储为 Excel 文件。这里是three save functions在模块级别:save_as 和 save_book_as。这是 how to save an array into an xls file 上的示例.

与pandas相比,pyexcel是一个轻量级、易于安装、简单易懂、基于组件的excel数据处理包。然而,它的目的并不是取代 pandas,而是为不太复杂的数据分析任务提供替代方案。

与 openpyxl 和 xlsxwriter 相比,pyexcel 可让您专注于数据,而不是文件格式,您可以重用代码来处理 ods、xls 和 csv 文件格式无需更改代码.

关于python - 使用 Pyexcel 处理 Excel 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43396437/

相关文章:

python - 如何比较python中的两个百分比?

java - 如何将属性类型更改为字符串(WEKA - CSV 到 ARFF)

excel - 宏运行时错误 '9' : subscript out of range

vba - 将excel power query中的2列数据转换为一个包含多列的标题行

python - 如何通过跳过 DataFrame 中的不可用日期来获取下面 Python DataFrame 的三天最高价、最低价和收盘价?

python - 如何在 tensorflow 中并行加载数据?

excel - 将自定义数字字段拆分字符串给出类型不匹配

ruby - 如何在编写 CSV 时跳过标题?

python - 修改 django 管理模板

python - 对变量内容进行数字排序