gpt4 book ai didi

python - 将 CSV 从两行转换为带有时间戳的列

转载 作者:行者123 更新时间:2023-11-30 22:42:14 25 4
gpt4 key购买 nike

我有一个 CSV 文件,格式为两行,其中包含 24 小时的数据:(逗号分隔。我在原来的问题中没有具体说明,抱歉!)

Site ID,Meter Reference,Date,Units,00:30,A,01:00,A,01:30,A,02:00,A,02:30,A,03:00,A,03:30,A,04:00,A,04:30,A,05:00,A,05:30,A,06:00,A,06:30,A,07:00,A,07:30,A,08:00,A,08:30,A,09:00,A,09:30,A,10:00,A,10:30,A,11:00,A,11:30,A,12:00,A,12:30,A,13:00,A,13:30,A,14:00,A,14:30,A,15:00,A,15:30,A,16:00,A,16:30,A,17:00,A,17:30,A,18:00,A,18:30,A,19:00,A,19:30,A,20:00,A,20:30,A,21:00,A,21:30,A,22:00,A,22:30,A,23:00,A,23:30,A,00:00,A
Building,A,12/06/15,kWh,1,A,2,A,2,A,1,A,2,A,2,A,1,A,2,A,2,A,1,A,2,A,2,A,1,A,2,A,2,A,1,A,2,A,1,A,2,A,2,A,2,A,1,A,2,A,2,A,0,A,1,A,0,A,1,A,3,A,2,A,2,A,1,A,0,A,0,A,0,A,1,A,0,A,0,A,0,A,1,A,0,A,0,A,0,A,1,A,0,A,0,A,0,A,1,A

我需要将其转换为可以在另一个进程中使用的格式。其中包含完整的时间戳、值(value)和建筑引用。我要把它推到一个 MySQL 表中。

Building,   Kwh,    Timestamp
A, 2, 12/06/15 00:30
A, 3, 12/06/15 01:00
A, 4, 12/06/15 01:30
A, 4, 12/06/15 02:00
A, 2, 12/06/15 02:30
A, 3, 12/06/15 03:00

我曾尝试使用它来旋转数据:

import csv

from itertools import izip
a = izip(*csv.reader(open("Logger.csv", "rb")))
csv.writer(open("Long.csv", "wb")).writerows(a)

但这给了我四行的标题和两行的数据。今天才开始使用 Python,我可以修改 import csv 使转换更清晰一些。

我尝试运行第二个 python 文件以附加每一行以添加时间戳并删除其中包含“A A”的行。我知道这不是正确的方法,正在寻求指导。

这是我目前的结果

Site ID Building
Meter Reference A
Date 11/06/15
Units kWh
00:30 2
A A
01:00 2
A A
01:30 2
A A
02:00 2
A A
02:30 2
A A
03:00 3

感谢任何帮助

最佳答案

这有效:

## Reading input
input_file = open("input_file_name.csv",'r')

input_data = []
for line in input_file:
input_data.append(line.split(";"))

date = input_data[1][4]
input_file.close()

## Writting output
output_file = open("output_file_name.csv",'w')

output_file.write("Building;Kwh;Timestamp\n")

swift = 6
size_data = len(input_data[0]) - swift
for i in range(size_data/2):
hour = input_data[0][swift + 2*i]
reference = input_data[0][swift + 2*i + 1]
kwh = input_data[1][swift + 2*i]
output_file.write(reference+";"+kwh+";"+date+" "+hour+"\n")

output_file.close()

关于python - 将 CSV 从两行转换为带有时间戳的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30807929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com