使用for循环从文本文件添加行并打印出结果-Python [英] Adding lines from a text file and printing out result using a for loop - Python

查看:58
本文介绍了使用for循环从文本文件添加行并打印出结果-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

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

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). 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). The files have a naming pattern of 'DECAY_COINC000.Spe','DECAY_COINC001.Spe' etc. Desired output below.

现有代码(最小示例)

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)

所需的输出

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

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).

因此,看起来像这样的输出会很好:(18,88),(36,73),(54,66),(72,55)...

So, an output that looks like this, would be fine: (18, 88), (36,73), (54,66), (72,55)...

当前输出

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.

推荐答案

解决方案是按如下方式更改'xmin = 745'之后的行:

Solution was to alter the lines after 'xmin = 745' as so:

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)

这篇关于使用for循环从文本文件添加行并打印出结果-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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