插入变量并使用python循环

时间:2018-06-05 01:52:33

标签: python python-3.x python-2.7 anaconda spyder

在data.txt中我有:

  1. LEW01,14 / 01 / 2016,07:50:00,15.6,-19,943,-8.7,-228,-19,943,-8.7,-0.9,48.5,4.14,12.39,
  2. LEW01,14 / 01 / 2016,08:00:00,16.2,-18,944,-8.6,-229,-19,943,-8.6,-0.9,48.5,4.14,12.39,
  3. 如何以这种方式插入Z:

    1. LEW01,14 / 01 / 2016,07:50:00,15.6,-19,943,-8.7,-228,-19,943,-8.7,-0.9,48.5,4.14,12.39, “Z”
    2. LEW01,14 / 01 / 2016,08:00:00,16.2,-18,944,-8.6,-229,-19,943,-8.6,-0.9,48.5,4.14,12.39, “Z”
    3. 使用循环,请??

      其中第1行中的Z为15.6*-19+943(第4列*第5列+第6列) 和 其中第2行中的Z为16.2*-18+944(第4列*第5列+第6列)

      之前感谢

1 个答案:

答案 0 :(得分:1)

您可以使用pandas库来操作表格数据。

In [1]: import pandas as pd

In [2]: %cat input.txt
LEW01,14/01/2016,07:50:00,15.6,-19,943,-8.7,-228,-19,943,-8.7,-0.9,48.5,4.14,12.39,
LEW01,14/01/2016,08:00:00,16.2,-18,944,-8.6,-229,-19,943,-8.6,-0.9,48.5,4.14,12.39,

In [3]: df = pd.read_csv('input.txt', header=None)

In [5]: df[15] = df[3] * df[4] + df[5]

In [10]: df.to_csv('output.txt', header=None, index=False)

In [11]: %cat output.txt
LEW01,14/01/2016,07:50:00,15.6,-19,943,-8.7,-228,-19,943,-8.7,-0.9,48.5,4.14,12.39,646.6
LEW01,14/01/2016,08:00:00,16.2,-18,944,-8.6,-229,-19,943,-8.6,-0.9,48.5,4.14,12.39,652.4