使用python在一列中减去2个数字

时间:2014-05-06 10:52:06

标签: python python-3.x

我有一个包含9列和14399行的文件。我希望,使用Python3.3,从同一列的第99列中减去第4列中的第一个数字,然后从第199列中减去第100个,然后在同一列中减去所有...并保存前两个的相应数字列和在新CSV文件中减去的答案。下面是表格示例和我的代码尝试(我搜索了问题,但没有找到任何问题)。

Input file named test.CAR
1/12/2009 00:00:05 01   34.51 1234.43 54.3  4321.5  55.4  3241.4
1/12/2009 00:00:10 02   34.51 1234.40 54.3  4321.52 55.4  3241.4
....
....
1/12/2009 00:10:05 99   36.51 4244.40 64.3  4421.52 85.4  4241.4
..
..
1/12/2009 00:20:10 100  44.51 1234.40 54.3  4321.52 55.4  3241.4
..
..
1/12/2009 00:30:10 199  54.51 1234.40 54.3  4321.52 55.4  3241.4
..
..
Output file named test.csv
1/12/2009 00:00:05  2.00               from (36.51-34.51)
1/12/2009 00:20:10 10.00               from (54.51-44.51)
..
..

到目前为止,这是我的代码:

import csv
import math

test_filename='C:/Python33/test.CAR'
test_filename=open(test_filename,'r')
num_lines=sum(1 for line in open('test.CAR'))
with open('test.csv','w',newline='')as fp:
    w=csv.writer(fp,delimiter=',')
    atad=[['DATE','TIME','NUMBER']]
    w.writerows(atad)
    a=0    #to set the first row
    d=98   ## to set the 99th row
    for i in range (1,(num_lines+1)):
        b=test_filename.readline()
        date=(b[0:10])   ## to capture the date in 1st column
        time=(b[12:19])  ## to capture the time in 2nd column
        y=b[24:30]       ## to capture the number I want in 4th column
        number=y(d)-y(a) ## to subtract the specific number on 1st from 99th column

        data=[[date,time,number]]
        w.writerows(data)
        a=a+98   ## counter to change 1st number to the 100th and so on 
        d=d+98   ## counter to change 99th number to the 199th and so on

test_filename.close()

代码无效,感谢您的帮助。谢谢!

2 个答案:

答案 0 :(得分:2)

将您的文件视为可迭代,并且可以轻松跳过行;我们可以使用itertools.islice()来跳过我们不需要的行:

from itertools import islice
import csv

test_filename = 'C:/Python33/test.CAR'

with open(test_filename, 'r') as infh, open('test.csv', 'w' ,newline='') as outfh:
    writer = csv.writer(outfh)
    writer.writerow(['DATE', 'TIME', 'NUMBER'])

    for line in infh:
        date1, time1, _, num1, _ = line.split(None, 4)
        num1 = float(num1)

        # skip 98 lines to read line number 99 from where we are now
        nextline = next(islice(infh, 98, 99), None)
        if nextline is None:
            break  # file is done early

        date2, time2, _, num2, _ = nextline.split(None, 4)
        num2 = float(num2)

        writer.writerow([date1, time1, num2 - num1])

这也使用float()将第4列转换为浮点值。它也使用writer.writerow()(单数)而不是writer.writerows()(复数),因为我们在这里一次只写一行。

答案 1 :(得分:0)

在不评论/更改任何样式选项的情况下,您的程序无法正常工作的原因是您访问列表y中尚未填写的值。

此外,您从文件中读取了数字,但此时它们仍然保存为字符串。

import csv
import math

test_filename='C:/Python33/test.CAR'
test_filename=open(test_filename,'r')
num_lines=sum(1 for line in open('test.CAR'))
with open('test.csv','w',newline='')as fp:
    w=csv.writer(fp,delimiter=',')
    atad=[['DATE','TIME','NUMBER']]
    w.writerows(atad)
    a=0    #to set the first row
    d=98   ## to set the 99th row
    for i in range (1,(num_lines+1)):
        b=test_filename.readline()
        date=(b[0:10])   ## to capture the date in 1st column
        time=(b[12:19])  ## to capture the time in 2nd column
        y=float(b[24:30])       ## to capture the number I want in 4th column
    while d < len(y)-1:
        number=y(d)-y(a) ## to subtract the specific number on 1st from 99th column

        data=[[date,time,number]]
        w.writerows(data)
        a=a+98   ## counter to change 1st number to the 100th and so on 
        d=d+98   ## counter to change 99th number to the 199th and so on

test_filename.close()

假设其余代码按预期工作(我完全不确定),引入y=float(b[24:30])while循环可以解决您的一些问题。