通过读取Python

时间:2016-02-09 00:05:31

标签: python parsing python-3.x

我正在尝试在python中编写一个程序,我们必须在其中添加来自不同类别和子类别的数字。该计划是关于农民每年从他的农场出售农产品。我们必须阅读的文本文件有4个类别。第一类是产品类型,例如蔬菜,水果,调味品。第二类告诉我们我们的产品类型,例如土豆,苹果,辣酱。第三类告诉我们2014年的销售情况,第四类告诉我们2015年的销售情况。在这个计划中,我们只需要计算2015年的总数。 2014年的数字存在于文本文件中,但无关紧要。

以下是文本文件的外观

PRODUCT,CATEGORY,2014 Sales,2015 Sales
Vegetables,Potatoes,4455,5644
Vegetables,Tomatoes,5544,6547
Vegetables,Peas,987,1236
Vegetables,Carrots,7877,8766
Vegetables,Broccoli,5564,3498
Fruits,Apples,398,4233
Fruits,Grapes,1099,1234
Fruits,Pear,2342,3219
Fruits,Bananas,998,1235
Fruits,Peaches,1678,1875
Condiments,Peanut Butter,3500,3902
Condiments,Hot Sauce,1234,1560
Condiments,Jelly,346,544
Condiments,Spread,2334,5644
Condiments,Ketchup,3321,3655
Condiments,Olive Oil,3211,2344

我们要做的是在2015年按产品添加销售额,然后再计算2015年所有产品的总销售额。

输出应该在书面文本文件中看起来像这样:

  

2015年蔬菜总销售额:{此处插入总数}

     

2015年水果的总销售额:{此处插入总数}

     

2015年调味品的总销售额:{此处插入总数}

           

2015年农民的总销售额:{插入所有人的总数   2015年销售的产品}

除此之外,它还应该在IDE的Python运行屏幕上打印总计和文本文件:

  

2015年农民的总销售额:{插入所有人的总数   2015年销售的产品}

这是我的代码。我是Python新手,阅读和编​​写文件所以我无法说出我是否走在正确的轨道上。

PRODUCT_FILE = "products.txt"
REPORT_FILE = "report.txt"

def main():
    #open the file
    productFile = open(PRODUCT_FILE, "r")
    reportFile = open(REPORT_FILE, "w")

    # reading the file
    proData = extractDataRecord(productFile)
    product = proData[0]
    category = proData[1]
    salesLastYear = prodata[2]
    salesThisYear = proData[3]

    #computing
    product = 0.0
    product = salesThisYear


    productFile.close()
    reportFile.close()

def extractDataRecord(infile) :
   line = infile.readline()
   if line == "" :
      return []
   else :
      parts = line.rsplit(",", 1)
      parts[1] = int(parts[1]) 
      return parts

# Start the program.
main()

2 个答案:

答案 0 :(得分:1)

你有一个csv文件,所以你应该使用pythons内置的csv模块来解析文件。 DictReader类将每一行转换为字典,其中键为标题。如果您的csv文件被称为import csv product_dict = {} cat_dict = {} with open('product_sales.csv', 'r') as f: for line in csv.DictReader(f): cat = line['CATEGORY'] product = line['PRODUCT'] sales_15 = int(line['2015 Sales']) if cat in cat_dict: cat_dict[cat] += sales_15 else: cat_dict[cat] = sales_15 if product in product_dict: product_dict[product] += sales_15 else: product_dict[product] = sales_15 Total = sum(cat_dict.values()) print product_dict print cat_dict print Total ,则以下代码将起作用。

{
    xtype: 'panel',
    layout: 'fit',
    items: [{
        xtype: "treepanel",
        scrollable: true
    }]
}

答案 1 :(得分:0)

您的代码看起来是一个好的开始;这里有几点建议:

  • 打开文件时使用productFile = open(PRODUCT_FILE, "r") # do something with the file productFile.close() 是个好主意,因为它可以保证它们能够正常关闭。而不是

    with open(PRODUCT_FILE, "r") as product_file:
        # do something with the file
    # the file has been closed!
    

    你应该做

    proData = extractDataRecord(productFile)
  • 您只需拨打while一次(即您获得标题行但没有数据)。你可以将它放在一个for line in product_file: product, category, _, sales = line.split(',') sales = int(sales) # now do something with the values! 循环中,但直接在文件上迭代更加惯用,即

    _

    (使用dict作为变量名称是&#34的简写;我不关心这个值")

  • 您可以使用product_sales = {} 跟踪每个产品和总销售额,

    for

    然后在product_sales[product] = product_sales.get(product, 0) + sales 循环中,

    from collections import defaultdict

    如果可以product_sales = defaultdict(int) product_sales[product] += sales ,这就更简单了:

    all_sales = 0
    for product, sales in product_sales.items():
        # write the sales for this product
        all_sales += sales
    # print all_sales
    
  • 处理完整个文件后,需要报告结果,如

    {{1}}