Python3-从所有文件中获取特定行的总和 [英] Python3 - getting the sum of a particular row from all the files

查看:78
本文介绍了Python3-从所有文件中获取特定行的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目录中有以下格式的许多文件:

I have many files in my directory of the below format:

name,sex,count
xyz,M,231
abc,F,654
...

我正在尝试获取所有文件的count(3nd coloumn)之和并将它们存储在列表中.

i am trying to get the sum of count(3rd coloumn) for all files and store them in a list.

total = []
result = 0
for filename in os.listdir(direc):
    if filename.endswith('.txt'):
        file = open(direc + '/' + filename, 'r')
        for line in file:
            line = line.strip()
            name, sex, count = line.split(',')
            if sex == 'F':
                result += int(count)
                total.append(result)

关于我的代码为什么不起作用的任何提示吗?

Any tips as to why my code doesn't work?

试图获取:

[sum(file1), sum(file2)...]

输入:

file1:
xyz,M,231
abc,F,654

file2:
wee,M,231
pol,F,654
bgt,M,434
der,F,543

file3:
wer,F,432
uio,M,124
poy,F,783

推荐答案

以下代码可实现绝对最少的修改(即未进行样式修复):

Here's code that works with the absolute bare minimum of modifications (that is, no style fixes were made):

total = []
for filename in os.listdir(direc):
    result = 0
    if filename.endswith('.txt'):
        file = open(direc + '/' + filename, 'r')
        for line in file:
            line = line.strip()
            try:
                name, sex, count = line.split(',')
            except ValueError:
                continue
            if sex == 'F':
                result += int(count)
    total.append(result)

以下问题必须解决:

  1. result 变量仅设置为零,而不是每个文件一次,因此读取的每个新文件都将添加到前一个文件的总数中.据我了解,您正在尝试将每个文件的结果添加到 total 列表中,因此我移动了这一行,以使该变量具有正确的结果.
  2. 名称,性别,计数= line.split(',')这行非常脆弱,每当一行中没有2个逗号的行(包括结尾的换行符)时,抛出一个错误.我将其包装在try ... except块中,该块捕获了这些错误并在需要时移至下一行.
  3. 在每次读取的行(而不是每个文件)上,结果都会附加到 total 列表中.
  1. The result variable was set to zero only once, not once per file, so each new file read kept adding to the previous file's total. By my understanding you are trying to add the result from each file to the total list, so I moved this line to make that variable have the correct result.
  2. The line name, sex, count = line.split(',') is very fragile, whenever a line has a line without 2 commas in it (including the closing newlines), it would throw an error. I wrapped it in a try…except block that catches these errors and moves on to the next line when needed.
  3. The result was appended to the total list on every line read, not per file.

如果我误解了您的意图,而您只是想在 total 变量中保留一个总计,以供参考,则只需进行修改#2.

If I misinterpreted your intentions and you just wanted to keep a running total in the total variable for reference, you only need to make modification #2.

这篇关于Python3-从所有文件中获取特定行的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆