在Python中解析两个文本文件以获得合并结果

时间:2015-11-04 22:05:13

标签: python parsing python-3.x function

一家巧克力公司决定对当前日期前30天生产的糖果产品提供折扣。我必须有一个矩阵作为打印结果,其中程序读取2个文件,一个是不同大小的不同糖果的成本,另一个是提供折扣的阈值天数。所以在这个问题中,两个文本文件看起来像这样

candies.txt

31 32 19 11 15 30 35 37
12 34 39 45 66 78 12 7
76 32 8 2 3 5 18 32 48
99 102 3 46 88 22 25 21
fd zz er 23 44 56 77 99 
44 33 22 55 er ee df 22

和第二个文件days.txt

30

但它可以有多个号码。它看起来像

30

40

36

所需的输出是

Discount at days = 30

      $  $  $         
$                $  $ 
      $ $ $ $  $      
       $       $  $  $ 
?  ?  ? $       
      $     ?  ?  ?   $      

Discount at days = 40

And then execute the output accordingly

所以基本上,无论数字都在days.txt中给出的数字之下,它应该打印一个"$"符号,并且在任何地方它都超过数字(在我们的例子中为30)它应该只在它们的位置打印空格。我们也有一个异常,我们在candies.txt矩阵中有英文字母,因为我们正在寻找数字来检查价格而不是字母,所以它应该在它们的位置打印"?"符号,因为它不是认可。

这是我的代码

def replace(word, threshold):

    try:
        value = int(word)
    except ValueError:
        return '?'
    if value < threshold:
        return '$'
    if value > threshold:
        return ' '
    return word

def get_threshold(filename):
    thresholds = []
    with open(filename) as fobj:
        for line in fobj:
            if line.strip():
               thresholds.append(int(line))
    return thresholds

def process_file(data_file, threshold):
    lines = []

    print('Original data:')
    with open(data_file) as f:
        for line in f:
            line = line.strip()
            print(line)

            replaced_line = '   '.join(
                replace(chunck, threshold) for chunck in line.split())
            lines.append(replaced_line)

    print('\nData replaced with threshold', threshold)
for threshold in get_threshold('days.txt'):
    process_file('demo.txt', threshold )

我的问题是,当第二个文件中只有一个数字days.txt时,我的代码可以工作,但是当第二个文件中有多个数字时它不起作用。我希望它在第二个文本文件的每个换行符中有多个数字时起作用。我不知道我做错了什么。

2 个答案:

答案 0 :(得分:1)

阅读所有门槛:

Core

没有列表理解的替代实现:

def get_thresholds(filename):
    with open(filename) as fobj :
        return [int(line) for line in fobj if line.strip()]

稍微修改你的功能:

def get_thresholds(filename):
    thresholds = []
    with open(filename) as fobj:
        for line in fobj:
            if line.strip():
               thresholds.append(int(line))
    return thresholds

完成所有门槛:

def process_file(data_file, threshold):
    lines = []

    print('Original data:')
    with open(data_file) as f:
        for line in f:
            line = line.strip()
            print(line)

            replaced_line = '   '.join(
                replace(chunck, threshold) for chunck in line.split())
            lines.append(replaced_line)

    print('\nData replaced with threshold', threshold)
    for line in lines:
        print(line)

答案 1 :(得分:0)

这是我之前回答的重写。由于长时间的讨论和许多变化,另一个答案似乎更清楚。我将任务切割成较小的子任务,并为每个任务定义一个函数。所有函数都有docstrings。强烈建议这样做。

"""
A chocolate company has decided to offer discount on the candy products
which are produced 30 days of more before the current date.

More story here ...
"""


def read_thresholds(filename):
    """Read values for thresholds from file.
    """
    thresholds = []
    with open(filename) as fobj:
        for line in fobj:
            if line.strip():
                thresholds.append(int(line))
    return thresholds


def read_costs(filename):
    """Read the cost from file.
    """
    lines = []
    with open(filename) as fobj:
        for line in fobj:
            lines.append(line.strip())
    return lines


def replace(word, threshold):
    """Replace value below threshold with `$`, above threshold with ` `,
       non-numbers with `?`, and keep the value if it equals the
       threshold.
    """
    try:
        value = int(word)
    except ValueError:
        return '?'
    if value < threshold:
        return '$'
    if value > threshold:
        return ' '
    return word


def process_costs(costs, threshold):
    """Replace the cost for given threshold and print results.
    """
    res = []
    for line in costs:
        replaced_line = '   '.join(
            replace(chunck, threshold) for chunck in line.split())
        res.append(replaced_line)

    print('\nData replaced with threshold', threshold)
    for line in res:
        print(line)


def show_orig(costs):
    """Show original costs.
    """
    print('Original data:')
    for line in costs:
        print(line)


def main(costs_filename, threshold_filename):
    """Replace costs for all thresholds and display results.
    """
    costs = read_costs(costs_filename)
    show_orig(costs)
    for threshold in read_thresholds(threshold_filename):
        process_costs(costs, threshold)


if __name__ == '__main__':
    main('candies.txt', 'days.txt')