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

使用python在一个文件的一列中减去2个数字,python,python-3.x,Python,Python 3.x,我有一个包含9列和14399行的文件。我想,使用Python3.3,从同一列的第99列中减去第4列的第一个数字,然后从第199列中减去第100列,如此等等…所有这些都在同一列中,并将前两列的相应数字和减法的答案保存在一个新的CSV文件中。下表是示例和我的代码尝试(我搜索了所有问题,但没有找到任何问题) 以下是我目前的代码: import csv import math test_filename='C:/Python33/test.CAR' test_filename=open(test_fi

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

以下是我目前的代码:

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

代码不起作用,我感谢你的帮助。谢谢

将文件视为可编辑文件,这样就很容易跳过行;我们可以使用跳过不需要的行:

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()
将第四列转换为浮点值。它还使用
writer.writerow()
(单数)而不是
writer.writerows()
(复数),因为我们在这里一次只写一行。

在不注释/更改任何样式选择的情况下,程序无法工作的原因是您访问了列表
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()
导入csv
输入数学
test\u filename='C:/Python33/test.CAR'
test\u filename=open(test\u filename,'r')
num_lines=sum(1表示线路处于开放状态('test.CAR'))
打开('test.csv','w',换行='')作为fp:
w=csv.writer(fp,分隔符=',')
atad=[['DATE','TIME','NUMBER']]
w、 writerows(atad)
a=0#设置第一行
d=98##设置第99行
对于范围(1)(num_行+1))中的i:
b=测试文件名.readline()
日期=(b[0:10])##在第1列中捕获日期
时间=(b[12:19])##在第二列中捕获时间
y=float(b[24:30])##在第4列中捕获我想要的数字
当d

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

您能比“不工作”更具体一点吗?错误(提供完整的回溯)?意外输出(提供输入以及预期和实际输出)?您好,我得到的错误是:number y(d)-y(a)…TypeError:'str'对象不可调用。可能您需要number=y[d]-y[a]好了
y
是一个六个字符的字符串,您试图用
d
作为参数来调用它,例如
y(d)
。我建议您将索引改为
y[d]-y[a]
,但使用
d==98
,这将导致
索引器
。你想用这行代码做什么?@user3178277如果你想在你的问题中添加更多的信息(什么是好的),比如回溯,最好是将它添加到你的原始问题中,然后添加到评论中,因为在你的问题中,任何试图帮助你的人都会看到它。Martijn Pieters…谢谢。我想就是这样。因此,如果我想从90减去第一个(而不是99),我需要将(infh,98,99),无)更改为(infh,89,90,无)?!如果这是正确的?如果减法值大于0.5,则仅记录如何?我想加上:如果num2-num1>0.5…write.writerow([date1,time1,num2-num1])。。。对吗??!!Martijn Pieters…我试过了,效果很好…非常非常感谢…你让我开心:)
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()