【发布时间】:2017-10-11 06:12:55
【问题描述】:
我有一个包含 6 列的 csv 文件..这是我正在执行操作的相同数据。
16 28-Sep-17 9700 291.8 291.8 9964.4
17 28-Sep-17 9700 186.3 186.3 9872.6
18 28-Sep-17 9700 166.6 166.6 9871.5
24 28-Sep-17 9800 227.5 227.5 9952.2
25 28-Sep-17 9800 202.55 202.55 9916.2
所以我想把 TV 和 tv 放到另一个文件中……格式如下所示……
9700 ** 9800**
TV/tv **TV/tv**
TV/tv **TV/tv**
我确实添加了writer = writerow(),但它正在奇怪地策划事情......
对于列名,我添加了这个条件C > 21,其中C 将在“9700”更改为 9800 时进行绘图....直到那时它应该在该列标题下添加数据....
我得到这样的输出......
9700
47.65
33.8
53.75
43.95
,,9800
68.2
49
76
62.3
希望大家理解
import csv
out = open('G:\out1.csv','w')
with open('G:\outpuop.csv', 'r+') as f:
reader = csv.reader(f)
writer = csv.writer(out)
y = list(reader)
L = len(y)
A = []
M = 0
c = 1
for x in range(1,L):
if c > 21:
print float(y[x][2])
c = 1
if (float(y[x][2])) > (float(y[x][5])) :
if ((float(y[x][4])) != 0 ):
TV = (float(y[x][4])) - ((float(y[x][2])) - (float(y[x][5])))
print TV
M = TV + M
c = c + 1
A.append(TV)
writer.writerow(A)
else:
TV = (float(y[x][3])) - ((float(y[x][2])) - (float(y[x][5])))
print TV
M = TV + M
c = c + 1
A.append(TV)
writer.writerow(A)
else:
if ((float(y[x][4])) != 0 ):
tv = (float(y[x][4]))
print tv
M = tv + M
c = c + 1
A.append(TV)
writer.writerow(A)
else:
tv = (float(y[x][3]))
print tv
M = tv + M
c = c + 1
A.append(TV)
writer.writerow(A)
print M
【问题讨论】:
-
嘿!,欢迎堆栈溢出。尝试隔离你的问题并写一些容易掌握的东西。您的问题涉及我们没有的数据,我们看不到预期的输出。我建议阅读:stackoverflow.com/help/how-to-ask
-
另外,您的编写器将无法工作,因为文件在
with块的末尾关闭。 -
@AntonvBR 嘿用数据解释了......我想要什么......希望我解释得很好......
-
@blakev 我将在
with之后移动那个作家,但没有得到期望的输出。 -
请根据您提供的数据显示您应该获得的确切输出。您要对数据进行哪些计算?