Python 根据for循环生成的列表的字符串条件求和值
我的代码搜索特定的文件,并调用一个单独的.py文件来输出一些数据。我手动为每个文件的文件大小添加了一行。我只想在迭代结束时附加找到的所有文件大小的总和。我想这将涉及使用布尔索引,但我找不到任何好的参考。我想找到所有标记为“文件大小”的列,然后对它们的所有值求和 在一个示例迭代中,我随机地将许多“文件大小”放在彼此相邻的位置,但在实际数据中,它们之间的间隔约为15行Python 根据for循环生成的列表的字符串条件求和值,python,python-3.x,csv,sum,export-to-csv,Python,Python 3.x,Csv,Sum,Export To Csv,我的代码搜索特定的文件,并调用一个单独的.py文件来输出一些数据。我手动为每个文件的文件大小添加了一行。我只想在迭代结束时附加找到的所有文件大小的总和。我想这将涉及使用布尔索引,但我找不到任何好的参考。我想找到所有标记为“文件大小”的列,然后对它们的所有值求和 在一个示例迭代中,我随机地将许多“文件大小”放在彼此相邻的位置,但在实际数据中,它们之间的间隔约为15行 xd = """Version 3.1.5.0 GetFileName C:\\users\\trinh\\downloads\\h
xd = """Version 3.1.5.0
GetFileName C:\\users\\trinh\\downloads\\higgi022_20150612_007_bsadig_100fm_aft_newIonTrap3.raw
GetCreatorID thermo
GetVersionNumber 64
file size 1010058
file size 200038
file size 48576986
file size 387905
misc tester
more python"""
在for循环结束时,我想对所有文件大小求和,这是非常错误的,但这是我最好的尝试:
zd = xd.split()
for aline in zd:
if 'file size' in aline:
sum = 0
for eachitem in aline[1:]:
sum += eaechitem
print(sum)
对于您给出的示例数据,要获取以文件大小开头的所有行的总数,可以执行以下操作:
xd = """Version 3.1.5.0
GetFileName C:\\users\\trinh\\downloads\\higgi022_20150612_007_bsadig_100fm_aft_newIonTrap3.raw
GetCreatorID thermo
GetVersionNumber 64
file size 1010058
file size 200038
file size 48576986
file size 387905
misc tester
more python"""
total = 0
for line in xd.splitlines():
if line.startswith('file size'):
total += int(line.split()[2])
print(total)
这将显示:
50174987
这首先将xd拆分为行,并为每行确定它是否以字file size开头。如果是这样,则使用“拆分”将线拆分为3个部分。第三部分包含字符串大小,因此需要使用int将其转换为整数
要扩展此功能以处理文件,首先需要读取文件并合计必要的行,然后以追加模式打开它以写入总计:
with open('data.txt') as f_input:
total = 0
for line in f_input:
if line.startswith('file size'):
total += int(line.split()[2])
with open('data.txt', 'a') as f_output:
f_output.write("\nTotal file size: {}\n".format(total))
根据当前脚本,您可以将其合并为以下内容:
import os
import csv
from subprocess import run, PIPE
pathfile = 'C:\\users\\trinh\\downloads'
msfilepath = 'C:\\users\\trinh\\downloads\\msfilereader.py'
file_size_total = 0
with open("output.csv", "w", newline='') as csvout:
writer = csv.writer(csvout, delimiter=',')
for root, dirs, files in os.walk(pathfile):
for f in files:
if f.endswith(".raw"):
fp = os.path.join(root, f) #join the directory root and the file name
p = run(['python', msfilepath, fp], stdout=PIPE) #run the MSfilereader.py path and each iterated raw file found
p = p.stdout.decode('utf-8')
for aline in p.split('\r\n'):
header = aline.split(' ', 1)
writer.writerows([header])
if 'END SECTION' in aline and aline.endswith('###'):
file_size = os.stat(fp).st_size
file_size_total += file_size
lst_filsz = ['file size', str(file_size)]
writer.writerow(lst_filsz)
writer.writerow(["Total file size:", file_size_total])
这将为您提供所有文件大小条目的总数。如果需要的话,还可以为每一款增加小计
注意,在使用with open….时,不必为文件添加关闭,只要您离开with语句的范围,文件就会自动关闭。试着想想您的具体问题是什么,并尽量减少帖子,使其只包含相关代码和信息SUMK??什么都不做。你好,我删掉了与问题无关的代码,并添加了一些注释。也许我想做的事情更清楚?嗨,马丁,谢谢你的回答,我感谢你的帮助。我相信你的代码可以工作,但我无法在现有代码中实现它。我使用for循环+writerows为从单独的.py文件中找到的每个文件生成数据,并在循环结束时使用if语句手动添加文件大小,该语句查找每个迭代的最后一行。我想这不是一个很好的技术,但是我得到了我想要的。但是,我不知道如何迭代“文件大小”行,因为我认为它并不存在。如果您使用csv,那么在关闭循环外的文件之前,只需添加如下内容:csv_output.writerow['total size',total],也许您可以将脚本复制到一个网站,例如,并在此处发布链接。嗨,Martin,下面是0bin.net托管的链接:不要认为您发布了正确的链接,那里只有一个JSON对象。