Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的增量整数_Python_Integer - Fatal编程技术网

python中的增量整数

python中的增量整数,python,integer,Python,Integer,我编写了一个简单的函数来逐行读取文件,进行一些计算,并将结果存储在另一个文件中。但是,当我在函数中输出count,而不是0、1、2、3;它变成了6.21263888889e-05、0.000141933611111等等。我想知道原因是什么 def CumulativePerSecond(input_file): # Record cumulative battery per second freq = 1000 # number of samples per second outpu

我编写了一个简单的函数来逐行读取文件,进行一些计算,并将结果存储在另一个文件中。但是,当我在函数中输出
count
,而不是0、1、2、3;它变成了6.21263888889e-05、0.000141933611111等等。我想知道原因是什么

def CumulativePerSecond(input_file):
  # Record cumulative battery per second
  freq = 1000 # number of samples per second
  output_file = os.path.splitext(input_file)[0] + "_1s" + ".txt"
  output = open(output_file, 'a+')
  count = 0
  for line2 in fileinput.input(input_file):
    count = count + 1
    print count
    if count == freq:
        output.write(str(line2))
        count = 0
  output.close()
部分输出:

1.87317876361

1.87321708889

1.87325520083

1.87329356889

1.87333199389

1.87337076056

1.87340823167

1.87344365278

1.87347473167

1.87351439528

1.87354390806

1.87362505778

我不知道应用程序的细节,但我会将数据发送到列表,然后使用Python的切片表示法选择每1000个数据点

请尝试以下代码段

data = range(100)
every_tenth = data[::10]
此外,如果将
关键字一起使用,则无需显式关闭文件

with open(filename) as f:
    data = f.readlines()
下面是您的代码以更“Pythonically”的方式重新编写——也就是说,它利用了Python通过C提供的一些快捷方式。您可能需要稍微修改一下才能让它运行。我不确定你是如何处理文件的

 def CumulativePerSecond(filename, freq=1000):
    data = []
    with open(filename) as input_file:
        data = input_file.readlines()

    output_file = os.path.splitext(filename)[0] + "_1s.txt"
    with open(out_filename, 'a+') as output:
        for line in data[::freq]:
            output.write(line)

您是否尝试过将类型转换为int(即,
count=int(count+1)
)?此外,您是否可以在它生成的序列中发布更多的数字,以提供有关产生错误的原因的更多信息。此外,您的主要目标是简单地输出一个文件,其中包含输入的每1000个数据点。这可以写得简单得多,而且您可能根本不需要count。我将添加更多输出。是的,我的目标是每1000次输出一次数据。您能告诉我是否有更简单的方法吗?切片表示法更简单
数据[::1000]