# !/usr/bin/env python # -*- coding:utf-8 -*- # author: haotian time:2019/9/14 import numpy as np f = open("./data/CD_Flight190914A.csv", "rb") excel = open("./data/time_flight.csv", "w+") # position_exl = open("./data/position_exl.csv", "w+") schedule = np.loadtxt(f, dtype=str, delimiter=",", skiprows=1, usecols=(4,)) # 分隔符 空格 Array = np.zeros(209) count = 1 i = 0 n = 0 while i < (len(schedule)-1): if schedule[i] == schedule[i + 1] : # 如果航班时间重复 创建一个不重复的时间表记录重复次数 count = count + 1 else: Array[n] = count #Array存的重复次数 count = 0 n = n + 1 i = i + 1 new_schedule,a = np.unique(schedule,return_index=True) #去掉相同时间的数据 # for i in range(len(position)): # position_exl.write(str(position[i])+',\n') # position_exl.close() # position_exl = open(("./data/position_exl.csv", "w+")) # position = np.loadtxt(position_exl, dtype=float, delimiter=",", skiprows=0, usecols=(0,)) # new_schedule = [len(position)*''] # n = 0 # numbers = [ int(x) for x in position ] # for i in range(numbers): # new_schedule[n] = schedule[i] # n = n + 1 excel.write("Schedule,PlaneNum"+'\n') for i in range(len(new_schedule)-1): excel.write(str(new_schedule[i])+","+str(Array[i])+",\n") excel.close() ''' 此时的数据time_flight.csv由于排序的原因导致时间的序列不一致, 最终数据用excel降序排列并保存到schedule_PlaneNum.csv中 '''完善并变为matlab上运行
时间: 2024-01-06 22:06:43 浏览: 146
你给出的代码似乎有些问题,例如在打开文件时需要指定文件编码方式,而且文件名中的斜杠应该使用双斜杠或者单斜杠。此外,你的代码中有一些注释会影响程序的执行,需要将其删除或者注释掉。下面是经过修改的代码:
```python
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: haotian
# time: 2019/9/14
import numpy as np
f = open(".\\data\\CD_Flight190914A.csv", "r", encoding='utf-8')
excel = open(".\\data\\time_flight.csv", "w+", encoding='utf-8')
# position_exl = open(".\\data\\position_exl.csv", "w+", encoding='utf-8')
schedule = np.loadtxt(f, dtype=str, delimiter=",", skiprows=1, usecols=(4,))
Array = np.zeros(209)
count = 1
i = 0
n = 0
while i < (len(schedule)-1):
if schedule[i] == schedule[i + 1]:
count = count + 1
else:
Array[n] = count
count = 0
n = n + 1
i = i + 1
new_schedule, a = np.unique(schedule, return_index=True)
# for i in range(len(position)):
# position_exl.write(str(position[i])+',\n')
# position_exl.close()
# position_exl = open(".\\data\\position_exl.csv", "w+", encoding='utf-8')
# position = np.loadtxt(position_exl, dtype=float, delimiter=",", skiprows=0, usecols=(0,))
# new_schedule = [len(position)*'']
# n = 0
# numbers = [int(x) for x in position]
# for i in range(numbers):
# new_schedule[n] = schedule[i]
# n = n + 1
excel.write("Schedule,PlaneNum\n")
for i in range(len(new_schedule)-1):
excel.write(str(new_schedule[i])+","+str(Array[i])+",\n")
excel.close()
'''此时的数据time_flight.csv由于排序的原因导致时间的序列不一致,
最终数据用excel降序排列并保存到schedule_PlaneNum.csv中
'''
```
你还需要将代码中的 `np.loadtxt` 函数改为适用于 MATLAB 的读取数据函数,例如 `csvread` 或者 `readtable`。同时,你需要将文件路径中的斜杠改为 MATLAB 上可用的反斜杠,例如 ".\\data\\time_flight.csv"。修改后的代码如下:
```matlab
f = csvread('.\data\CD_Flight190914A.csv', 1, 3);
excel = fopen('.\data\time_flight.csv', 'w');
schedule = f(:, 2);
Array = zeros(209, 1);
count = 1;
i = 1;
n = 1;
while i < length(schedule)
if strcmp(schedule(i), schedule(i + 1))
count = count + 1;
else
Array(n) = count;
count = 1;
n = n + 1;
end
i = i + 1;
end
new_schedule = unique(schedule);
fprintf(excel, 'Schedule,PlaneNum\n');
for i = 1:length(new_schedule)
fprintf(excel, '%s,%d\n', new_schedule{i}, Array(i));
end
fclose(excel);
```
注意,在 MATLAB 中,字符串数组的索引从 1 开始,而不是从 0 开始,因此在代码的循环部分需要将 `i` 的初始值设为 1。同时,MATLAB 中的字符串比较需要使用 `strcmp` 函数。在输出 CSV 文件时,可以使用 `fprintf` 函数来控制格式。
阅读全文