python實現代碼如下
import csv
import datetime
import pandas as pd
if __name__ == '__main__':
## 文件路徑
csv_path = './data/ganglia-metrics.csv'
## 導出文件命名
new_csv_name = "new_ganglia-metrics.csv"
## 讀取csv信息
data = pd.read_csv(csv_path)
## 我就只有兩列數據,就定義了兩個數組,處理完存放再數組裡,最後再保存輸出
timestampList = []
cpu_idleList = []
# 修改csv某列數據信息
for i in range(data.shape[0]):
# 修改格式
t = (datetime.datetime.strptime(data.loc[i]['Timestamp'], "%Y-%m-%dT%H:%M:%S+00:00") + datetime.timedelta(
hours=2)).strftime("%Y-%m-%d %H-%M-%S")
# 寫入數組
timestampList.append(t)
cpu_idleList.append(data['cpuIdle'][i])
# 寫出到csv
with open(new_csv_name, "w", newline="") as csvfile:
writer = csv.writer(csvfile)
# 先寫入columns_name
writer.writerow(["Timestamp", "cpuIdle"])
for i in range(len(timestampList)):
writer.writerow([timestampList[i], cpu_idleList[i]])