繁体   English   中英

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

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

问题

我有一组 50 个文件,其中包含 8192 行整数(跳过前 12 行无关紧要后,底部还有 12 行可以跳过)。 我只对每个文件中 70 行的补丁感兴趣(第 745-815 行,或者如果您在开头包含 12 行,则为 757-827)。 这些文件的命名模式为“DECAY_COINC000.Spe”、“DECAY_COINC001.Spe”等。下面是所需的 output。

现有代码(最小化示例)

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)

所需 Output

目标是在每一行上添加数字以找到 xmin 和 xmax 之间的总计数,然后打印出来,以及一个数字来指定它来自哪个文件(这里我使用 18 * 文件数+1) .

因此,看起来像这样的 output 就可以了:(18, 88), (36,73), (54,66), (72,55)...

当前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.]

...这持续了很多行。 老实说,我真的不明白 output 发生了什么。

解决方案是更改 '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