Python 尝试从拆分列表中添加特定列

Python 尝试从拆分列表中添加特定列,python,Python,我有一个需要拆分的输入文件。文件可以有任意数量的行,但每行有4个内容。首先是地区代码,其次是该地区销售的小说书籍数量,其次是该地区销售的非小说书籍数量,最后是该地区的税收(例如:TX 493 515 0.055)。我已经弄清楚了我需要做的所有事情,除了总结所有的小说、非小说类书籍和总销量。假设总共只有三行,每个地区的小说销量分别为49350489本,显然,它们都是独立的。以下是我写的,我想知道我做错了什么: while (myFile != ""): myFile = myFile.s

我有一个需要拆分的输入文件。文件可以有任意数量的行,但每行有4个内容。首先是地区代码,其次是该地区销售的小说书籍数量,其次是该地区销售的非小说书籍数量,最后是该地区的税收(例如:TX 493 515 0.055)。我已经弄清楚了我需要做的所有事情,除了总结所有的小说、非小说类书籍和总销量。假设总共只有三行,每个地区的小说销量分别为49350489本,显然,它们都是独立的。以下是我写的,我想知道我做错了什么:

while (myFile != ""):

    myFile = myFile.split()
    sumFiction = 0
    for i in range(myFile):
        sumFiction = sumFiction + eval(myFile[1])
如果我拆分了(CO 493 515 0.055)的文件,则CO不是我的文件[0],493不是我的文件[1],等等。如有任何帮助,将不胜感激

编辑:对不起,我应该说得更具体一点。我正在读取一个文件,假设这个文件有3行(但我的代码需要无限行):

首先是地区代码,然后是小说销量,然后是非小说类图书销量,然后是该地区的税收。我需要计算出该地区的图书销售总额等,我已经做了。我唯一搞不懂的是如何总结售出的所有三行小说(例:415330401)。以下是迄今为止的代码:

def ComputeSales(fictionBooks,nonFictionBooks,areaTax):
    total = (fictionBooks * 14.95) + (nonFictionBooks * 9.95)
    tax = total * areaTax
    totalSales = total + tax

    return total,tax,totalSales

def main():
    #inFile = input("Please enter name of book data file:  ")
    #inFile = open(inFile,"r")
    inFile = open("pa7.books","r")
    myFile = inFile.readline()

    print()
    print("{0:14}{1:10}".format("","Units Sold"))
    print("{0:10}{1:11}{2:17}{3:12}{4:8}{5:11}".format(
                "Region","Fiction","Non-Fiction","Total","Tax","Total Sales"))
    print("---------------------------------------------------------------------")

    while (myFile != ""):
        myFile = myFile.split()
        sumFiction = 0
        #for i in range(myFile):
            #sumFiction = sumFiction + eval(myFile[1])

        total,tax,totalSales = ComputeSales(eval(myFile[1]),eval(myFile[2]),eval(myFile[3]))

        print("{0:2}{1:10}{2:13}{3:4}{4:14.2f}{5:10.2f}{6:16.2f}".format(
                   "",myFile[0],myFile[1],myFile[2],total,tax,totalSales))

        myFile = inFile.readline()

     print("---------------------------------------------------------------------")
    #print("{0:11}{1:13}{2:34}{3:2}{4:8}".format(
    #             "Total","15035","3155","$","272843.41"))
    print(sumFiction)

main()

Edit:好的,我之前的回答是基于假设
myFile
实际上是一个文件对象,而不是一行文件

您的主要问题似乎是您试图在另一个循环中执行一个循环,这并没有真正意义:您只需要在文件的行上执行一个循环,并将每行的总数相加

下面是您的
main
函数的编辑版本。我还:

  • 切换到文件上的
    循环,因为这更自然
  • 按照注释中的建议,使用
    float
    代替
    eval
    ,这样恶意或错误的数据文件只会使程序崩溃,而不会运行任意代码
  • 切换到使用
    with
    语句打开文件:这保证了即使程序中途崩溃,文件也会被关闭,这是一个好习惯,尽管在这里没有太大区别
  • 将变量名切换到标准Python样式
    snake\u case
    样式,而不是
    camelCase
    。(另外,
    ComputeSales
    通常是
    compute\u sales
    CamelCase
    名称通常仅用于类名。)
  • 将文件名更改为参数,以便您可以使用例如
    main(sys.argv[1]if len(sys.argv)>1 else“pa7.books”)调用它以支持命令行参数
这是:

def main(filename="pa7.books"):
    sum_fiction = 0
    sum_nonfiction = 0
    sum_total = 0

    with open(filename) as in_file:
        for line in in_file:
            if not line.strip():
                 continue # skip any blank lines

            fields = line.split()
            region = fields[0]
            fiction, nonfiction, area_tax = [float(x) for x in fields[1:]]

            total, tax, total_sales = ComputeSales(fiction, nonfiction, area_tax)

            sum_fiction += fiction
            sum_nonfiction += nonfiction
            sum_total += total_sales

    print("{0:2}{1:10}{2:13}{3:4}{4:14.2f}{5:10.2f}{6:16.2f}".format(
           "", region, fiction, nonfiction, total, tax, total_sales))

    print("---------------------------------------------------------------------")
    print("{0:11}{1:13}{2:34}{3:2}{4:8}".format(
           "Total", sum_fiction, sum_nonfiction, "$", sum_total))

如果您不理解我建议的任何更改,请随时询问

编辑:好的,我之前的回答是基于假设
myFile
实际上是一个文件对象,而不是一行文件

您的主要问题似乎是您试图在另一个循环中执行一个循环,这并没有真正意义:您只需要在文件的行上执行一个循环,并将每行的总数相加

下面是您的
main
函数的编辑版本。我还:

  • 切换到文件上的
    循环,因为这更自然
  • 按照注释中的建议,使用
    float
    代替
    eval
    ,这样恶意或错误的数据文件只会使程序崩溃,而不会运行任意代码
  • 切换到使用
    with
    语句打开文件:这保证了即使程序中途崩溃,文件也会被关闭,这是一个好习惯,尽管在这里没有太大区别
  • 将变量名切换到标准Python样式
    snake\u case
    样式,而不是
    camelCase
    。(另外,
    ComputeSales
    通常是
    compute\u sales
    CamelCase
    名称通常仅用于类名。)
  • 将文件名更改为参数,以便您可以使用例如
    main(sys.argv[1]if len(sys.argv)>1 else“pa7.books”)调用它以支持命令行参数
这是:

def main(filename="pa7.books"):
    sum_fiction = 0
    sum_nonfiction = 0
    sum_total = 0

    with open(filename) as in_file:
        for line in in_file:
            if not line.strip():
                 continue # skip any blank lines

            fields = line.split()
            region = fields[0]
            fiction, nonfiction, area_tax = [float(x) for x in fields[1:]]

            total, tax, total_sales = ComputeSales(fiction, nonfiction, area_tax)

            sum_fiction += fiction
            sum_nonfiction += nonfiction
            sum_total += total_sales

    print("{0:2}{1:10}{2:13}{3:4}{4:14.2f}{5:10.2f}{6:16.2f}".format(
           "", region, fiction, nonfiction, total, tax, total_sales))

    print("---------------------------------------------------------------------")
    print("{0:11}{1:13}{2:34}{3:2}{4:8}".format(
           "Total", sum_fiction, sum_nonfiction, "$", sum_total))
如果您不理解我建议的任何更改,请随时询问

啊。这太难看了。我不再主要是Python程序员了,因此可能有更好的工具来实现这一点。但是,让我们从概念层面来解决这个问题

这是标准的命令式编程,使问题过于复杂。它很容易在实现噪音中迷失方向,这正是您面临的问题。它使你看不见森林,看不见树木。让我们尝试另一种方法

让我们把重点放在我们需要做的事情上,并让实现由此产生。首先,我们知道我们需要从文件中读取

从文件中读取 以下是作为测试用例的Python实现:

import unittest

class RegionTests(unittest.TestCase):
    def testLoadARegionDatabase(self):
        """Given a region file,when I load it, then it should be stored in memory"""
        # Given region database
        regionDatabase = []
        # When I load it
        with open('./regions.txt','r') as f:
            regionDatabase = f.readlines()
        # Then contents should be available
        self.assertTrue(len(regionDatabase) > 0)
def testCreateRegionFromData(self):
        """Given a set of data, when I create a region, then its non-fiction sales, fiction sales,and tax rate should be set"""
        # Given a set of data
        texas = { "regionCode": "TX", "fiction" : 415, "nonfiction" : 555, "taxRate" : 0.55 }
        # When I create a region
        region = Region(texas["regionCode"], texas["fiction"], texas["nonfiction"], texas["taxRate"])
        # Then its attributes should be set
        self.assertEquals("TX", region.code)
        self.assertEquals(415, region.fiction)
        self.assertEquals(555, region.nonfiction)
        self.assertEquals(0.55, region.taxRate)
def testGetTotalSalesForRegion(self):
        """Given a region with fiction and nonfiction sales, when I ask for its total sales, then I should get the result"""
        # Given a set of data
        texas = { "regionCode": "TX", "fiction" : 415, "nonfiction" : 555, "taxRate" : 0.55 }
        region = Region("TX", 415, 555, 0.55)
        # When I ask the region for its total sales
        result = region.totalSales()
        # Then I should get the sum of the sales
        self.assertEquals(970,result)
从文件中获取区域数据 从概念上讲,我们知道该文件中的每一行都有其含义。基本上,每一行都是一个区域。我们在文件中存储了代码、虚构销售、非虚构销售和税率。区域的概念应该在我们的系统中有一个明确的、一流的表示,因为

以下是作为测试用例的Python实现:

import unittest

class RegionTests(unittest.TestCase):
    def testLoadARegionDatabase(self):
        """Given a region file,when I load it, then it should be stored in memory"""
        # Given region database
        regionDatabase = []
        # When I load it
        with open('./regions.txt','r') as f:
            regionDatabase = f.readlines()
        # Then contents should be available
        self.assertTrue(len(regionDatabase) > 0)
def testCreateRegionFromData(self):
        """Given a set of data, when I create a region, then its non-fiction sales, fiction sales,and tax rate should be set"""
        # Given a set of data
        texas = { "regionCode": "TX", "fiction" : 415, "nonfiction" : 555, "taxRate" : 0.55 }
        # When I create a region
        region = Region(texas["regionCode"], texas["fiction"], texas["nonfiction"], texas["taxRate"])
        # Then its attributes should be set
        self.assertEquals("TX", region.code)
        self.assertEquals(415, region.fiction)
        self.assertEquals(555, region.nonfiction)
        self.assertEquals(0.55, region.taxRate)
def testGetTotalSalesForRegion(self):
        """Given a region with fiction and nonfiction sales, when I ask for its total sales, then I should get the result"""
        # Given a set of data
        texas = { "regionCode": "TX", "fiction" : 415, "nonfiction" : 555, "taxRate" : 0.55 }
        region = Region("TX", 415, 555, 0.55)
        # When I ask the region for its total sales
        result = region.totalSales()
        # Then I should get the sum of the sales
        self.assertEquals(970,result)
这是失败的。让我们通过吧

class Region:
    def __init__(self, code, fiction, nonfiction,rate):
        self.code = code
        self.fiction = fiction
        self.nonfiction = nonfiction
        self.taxRate = rate
class Analyst:
    def __init__(self,regions):
        self.regions = regions

    def calculateTotalNonFictionSales(self):
        return sum([reg.nonfiction for reg in self.regions])
分析总数 现在我们知道我们的系统可以表示区域。我们想要的东西,可以分析一系列地区,并给我们的销售汇总统计数字。让我们称之为分析师

Feature: Calculate Total Sales

As a user, in order to be able to know what is going on, I want to be able to ask an Analyst what the total sales are for my region

Given: I have a set of regions
When : I ask my Analyst what the total sales are
Then : The analyst should return me the correct answers
下面是作为测试用例的Python实现

def testAnalyzeRegionsForTotalNonFictionSales(self):
    """Given a set of Region, When I ask an Analyst for total non-fiction sales, then I should get the sum of non-fiction sales"""
    # Given a set of regions
    regions = [ Region("TX", 415, 555, 0.55), Region("MN", 330, 999, 0.78), Region("HA", 401, 674, 0.99) ]
    # When I ask my analyst for the total non-fiction sales
    analyst = Analyst(regions)
    result = analyst.calculateTotalNonFictionSales()
    self.assertEquals(2228, result)
这是失败的。让我们通过吧

class Region:
    def __init__(self, code, fiction, nonfiction,rate):
        self.code = code
        self.fiction = fiction
        self.nonfiction = nonfiction
        self.taxRate = rate
class Analyst:
    def __init__(self,regions):
        self.regions = regions

    def calculateTotalNonFictionSales(self):
        return sum([reg.nonfiction for reg in self.regions])
你应该可以从这里推断小说的销量
from region import Region
from analyst import Analyst

def main():
   text = readFromRegionFile()
   regions = createRegionsFromText(text)
   analyst = Analyst(regions)
   printResults(analyst)

def readFromRegionFile():
    regionDatabase = []
    with open('./regions.txt','r') as f:
            regionDatabase = f.readlines()
    return regionDatabase

def createRegionsFromText(text):
    regions = []
    for line in text:
        data = line.split()
        regions.append(Region(data[0],data[1], data[2], data[3]))
    return regions

def printResults(analyst):
    totSales = analyst.calculateTotalSales()
    totFic = analyst.calculateTotalFictionSales()
    totNon = analyst.calculateTotalNonFictionSales()
    for r in analyst.regions:
        print("{0:2}{1:10}{2:13}{3:4}{4:14.2f}{5:10.2f}".format(
           "", r.code, r.fiction, r.nonfiction, r.totalSales(), r.taxRate))

    print("---------------------------------------------------------------------")
    print("{0:11}{1:13}{2:34}{3:2}{4:8}".format(
           "Total", totFic, totNon, "$", totSales))

if __name__ == "__main__":
    main()