#Grid_solar_EV_data.txt文件保存了NY,Austin,Boulder三个城市的电网,太阳能以及电动汽车数据,编程实现将文件按NY,Austin,Boulder
#三个城市划分,将数据划分成三个文件,并以txt文件格式存储,其中分割后文件中的数据顺序,结构和表头与源文件一致,且每个文件的第一列为
#Datetime列,NY数据保存为1.txt,Austin数据保存为2.txt,Boulder数据保存为3.txt。
#1 读取数据
f = open("Grid_solar_EV_data.txt",'r')
rows = []
for lines in f.readlines():#读取每一行数据
lines = lines.split('\t')#拆分
rows.append(lines)
f.close()
#2 提取数据
NY = []
Austin = []
Boulder = []
[NY.append(row[:4]) for row in rows]#前四列为NY数据
[Austin.append(row[0:1]+row[4:7]) for row in rows]#五到七列为Austin数据
[Boulder.append(row[0:1]+row[7:]) for row in rows]#八到十列为Boulder数据
#3 保存数据
with open('1.txt','a+') as txtFile:
#依次添加每一行的数据
for i in NY:
for j in i:
txtFile.writelines(j)
txtFile.writelines(' ')#分割符
txtFile.writelines('\n')#换行
with open('2.txt','a+') as txtFile:
#依次添加每一行的数据
for i in Austin:
for j in i:
txtFile.writelines(j)
txtFile.writelines(' ')#分割符
txtFile.writelines('\n')#换行
with open('3.txt','a+') as txtFile:
#依次添加每一行的数据
for i in Boulder:
for j in i:
txtFile.writelines(j)
txtFile.writelines(' ')#分割符
txtFile.writelines('\n')#换行
Original data: Grid_solar_EV_data.txt file
Link: https: //pan.baidu.com/s/1S02i-kbkTvaBcsOlSTDg1w
extraction code: vmb9