简体   繁体   English

从文本文件中添加行并使用 for 循环打印出结果 - Python

[英]Adding lines from a text file and printing out result using a for loop - Python

Problem问题

I have a set of 50 files, which contain 8192 lines of integers (after skipping the first 12, which are irrelevant, there are also 12 more lines at the bottom which can be skipped).我有一组 50 个文件,其中包含 8192 行整数(跳过前 12 行无关紧要后,底部还有 12 行可以跳过)。 I am only interested in a patch of 70 lines in each file (lines 745-815, or 757-827 if you include the 12 at the start).我只对每个文件中 70 行的补丁感兴趣(第 745-815 行,或者如果您在开头包含 12 行,则为 757-827)。 The files have a naming pattern of 'DECAY_COINC000.Spe','DECAY_COINC001.Spe' etc. Desired output below.这些文件的命名模式为“DECAY_COINC000.Spe”、“DECAY_COINC001.Spe”等。下面是所需的 output。

Existing Code (Minimalised Example)现有代码(最小化示例)

import numpy as np
from numpy import exp, loadtxt, pi, sqrt, random, linspace
import glob, os

## Define constants
fileToRun = 'Run0'
location = 'ControlRoom6'
approxcen = 780
DeadTime = 3
LiveTime = 15
width = 1

## Get location of files to be loaded
folderId = '\\'
baseFolder = 'C:'+folderId+'Users'+folderId+location+folderId+'Documents'+folderId+'PhD'+folderId+'Ubuntu-Analysis-DCF'+folderId+'DCF-an-b+decay'+folderId+'dcp-ap-27Al'+folderId+''
prefix = 'DECAY_COINC'

## Define paramaters
folderToAnalyze = baseFolder + fileToRun + '\\'
MaestroT = LiveTime + DeadTime
definiteintegralprints = 1

## Gets number of files
files = []
os.chdir(folderToAnalyze)
for file in glob.glob(prefix + "*.Spe"):
    files.append(file)
numfiles = len(files)
if numfiles<=1:
    print('numfiles is {0}, minimum of 2 is required'.format(numfiles))
    raise SystemExit(0)

if definiteintegralprints == 1:   
    print("Commence printing counts in fixed interval sequence")
    
    xmin = 745
    xmax = 815
    
    for n in range(0, numfiles):
    
        x = np.linspace(0, 8191, 8192)
        finalprefix = str(n).zfill(3)
        fullprefix = folderToAnalyze + prefix + finalprefix
        y = loadtxt(fullprefix + ".Spe", skiprows= xmin+12, max_rows = xmax-xmin) 
    
        
        for x in range(xmin,xmax):
            count = count + y
            time = MaestroT*(n+1)
            
            print(time, count)

Desired Output所需 Output

The target is to add the number on each of those lines to find the total count between xmin and xmax, and print out, alongside with a number to designate which file it came from (here I use 18 * number of the file+1).目标是在每一行上添加数字以找到 xmin 和 xmax 之间的总计数,然后打印出来,以及一个数字来指定它来自哪个文件(这里我使用 18 * 文件数+1) .

So, an output that looks like this, would be fine: (18, 88), (36,73), (54,66), (72,55)...因此,看起来像这样的 output 就可以了:(18, 88), (36,73), (54,66), (72,55)...

Current Output当前Output

Commence printing counts in fixed interval sequence
18 [   0.  210.    0.  281.   70.  141.   70.  490.  140.  280.  351.  700.
  700.  491.  912.  492.  561.  633.  632.  630.  912. 1473. 1401. 1191.
 1260. 1262. 1401.  981. 1542. 1260. 1682. 1122. 1960. 1263. 2241. 1821.
 2102. 1471. 2242. 2103. 1471. 1892. 1403. 1822. 1400. 1331.  983. 1333.
  910. 1262.  980.  702.  772.  702.  700.  212.  561.  351.  843.  490.
  140.  280.  140.    0.   70.  210.    0.    0.  140.   70.]
18 [   0.  210.    0.  282.   70.  142.   70.  490.  140.  280.  352.  700.
  700.  492.  914.  494.  562.  636.  634.  630.  914. 1476. 1402. 1192.
 1260. 1264. 1402.  982. 1544. 1260. 1684. 1124. 1960. 1266. 2242. 1822.
 2104. 1472. 2244. 2106. 1472. 1894. 1406. 1824. 1400. 1332.  986. 1336.
  910. 1264.  980.  704.  774.  704.  700.  214.  562.  352.  846.  490.
  140.  280.  140.    0.   70.  210.    0.    0.  140.   70.]
18 [   0.  210.    0.  283.   70.  143.   70.  490.  140.  280.  353.  700.
  700.  493.  916.  496.  563.  639.  636.  630.  916. 1479. 1403. 1193.
 1260. 1266. 1403.  983. 1546. 1260. 1686. 1126. 1960. 1269. 2243. 1823.
 2106. 1473. 2246. 2109. 1473. 1896. 1409. 1826. 1400. 1333.  989. 1339.
  910. 1266.  980.  706.  776.  706.  700.  216.  563.  353.  849.  490.
  140.  280.  140.    0.   70.  210.    0.    0.  140.   70.]

...This goes on for many many lines. ...这持续了很多行。 I don't really understand what's going on in the output if I am honest.老实说,我真的不明白 output 发生了什么。

Solution was to alter the lines after 'xmin = 745' as so:解决方案是更改 'xmin = 745' 之后的行,如下所示:

xmin = 745
xmax = 815
skip = 12

for n in range(0, numfiles):

    total = 0
    
    x = np.linspace(0, 8191, 8192)
    finalprefix = str(n).zfill(3)
    fullprefix = folderToAnalyze + prefix + finalprefix
    y = loadtxt(fullprefix + ".Spe", skiprows= xmin+skip, max_rows = xmax-xmin)
   
    for x in y:
        val = int(x)
        total = total + val
    
    print(((n+1)*MaestroT), total)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM