Python将数据线连接在一起

时间:2016-09-30 07:34:28

标签: python python-2.7 dataset

你好我有几千行的数据集,它们被分成偶数和奇数行,我找不到在同一行再次将它们连接在一起的方法。读取文件并覆盖它很好或者创建一个新文件。

我发现这个example打印单独的行,但无法将其写入文件。

我希望它看起来像这样:

Time = 1 Temperature1 = 24.75 Temperature2 = 22.69 Temperature3 = 20.19 RPM = -60.00
Time = 2 Temperature1 = 24.75 Temperature2 = 22.75 Temperature3 = 20.19 RPM = -60.00
etc...

数据集示例:

Time = 1 Temperature1 = 24.75 Temperature2 = 22.69 Temperature3 = 20.19
 RPM = -60.00
Time = 2 Temperature1 = 24.75 Temperature2 = 22.75 Temperature3 = 20.19
 RPM = -60.00
Time = 3 Temperature1 = 24.75 Temperature2 = 22.75 Temperature3 = 20.19
 RPM = -60.00
Time = 4 Temperature1 = 24.81 Temperature2 = 22.81 Temperature3 = 20.25
 RPM = -60.00
Time = 5 Temperature1 = 24.81 Temperature2 = 22.81 Temperature3 = 20.19
 RPM = -60.00
Time = 6 Temperature1 = 24.81 Temperature2 = 22.81 Temperature3 = 20.19
 RPM = -60.00
Time = 7 Temperature1 = 24.81 Temperature2 = 22.81 Temperature3 = 20.25
 RPM = -60.00
Time = 8 Temperature1 = 24.81 Temperature2 = 22.87 Temperature3 = 20.25
 RPM = -60.00
Time = 9 Temperature1 = 24.87 Temperature2 = 22.87 Temperature3 = 20.25
 RPM = -60.00
Time = 10 Temperature1 = 24.87 Temperature2 = 22.87 Temperature3 = 20.25
 RPM = -60.00

1 个答案:

答案 0 :(得分:0)

您可以使用%(模数)来确定线条是奇数还是偶数。如果它是偶数,则将最后一行和当前行连接在一起。

# Using your dataset as a string
data_split = data.split("\n")

for i in range(len(data_split)):
    if i % 2:
        lines = [data_split[i-1], data_split[i]]
        print " ".join(lines)
  

输出:

     

时间= 1温度1 = 24.75温度2 = 22.69温度3 =   20.19 RPM = -60.00

     

时间= 2温度1 = 24.75温度2 = 22.75温度3 =   20.19 RPM = -60.00

     

时间= 3温度1 = 24.75温度2 = 22.75温度3 =   20.19 RPM = -60.00

     

...