# !/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 20:06:43 浏览: 26
你给出的代码似乎有些问题,例如在打开文件时需要指定文件编码方式,而且文件名中的斜杠应该使用双斜杠或者单斜杠。此外,你的代码中有一些注释会影响程序的执行,需要将其删除或者注释掉。下面是经过修改的代码: ```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` 函数来控制格式。

相关推荐

最新推荐

recommend-type

解决-BASH: /HOME/JAVA/JDK1.8.0_221/BIN/JAVA: 权限不够问题

主要介绍了解决-BASH: /HOME/JAVA/JDK1.8.0_221/BIN/JAVA: 权限不够的问题,需要的朋友可以参考下
recommend-type

MySQL 启动报错:File ./mysql-bin.index not found (Errcode: 13)

主要介绍了MySQL 启动报错:File ./mysql-bin.index not found (Errcode: 13)的解决方法,需要的朋友可以参考下
recommend-type

linux输入yum后提示: -bash: /usr/bin/yum: No such file or directory的解决方法

在本篇文章里小编给大家整理的是关于linux输入yum后提示: -bash: /usr/bin/yum: No such file or directory的解决方法,有需要的朋友们参考下。
recommend-type

64位linux 编译c提示gnu/stubs-32.h:No such file or directory的解决方法

主要介绍了64位linux 编译c提示gnu/stubs-32.h:No such file or directory的解决方法,需要的朋友可以参考下
recommend-type

基于risc-V的五级流水线CPU实验项目源码+文档说明.zip

基于risc-V的五级流水线CPU实验项目源码+文档说明.zip已获导师指导并通过的97分的高分课程设计项目,可作为课程设计和期末大作业,下载即用无需修改,项目完整确保可以运行。 基于risc-V的五级流水线CPU实验项目源码+文档说明.zip已获导师指导并通过的97分的高分课程设计项目,可作为课程设计和期末大作业,下载即用无需修改,项目完整确保可以运行。 基于risc-V的五级流水线CPU实验项目源码+文档说明.zip已获导师指导并通过的97分的高分课程设计项目,可作为课程设计和期末大作业,下载即用无需修改,项目完整确保可以运行。 基于risc-V的五级流水线CPU实验项目源码+文档说明.zip已获导师指导并通过的97分的高分课程设计项目,可作为课程设计和期末大作业,下载即用无需修改,项目完整确保可以运行。 基于risc-V的五级流水线CPU实验项目源码+文档说明.zip已获导师指导并通过的97分的高分课程设计项目,可作为课程设计和期末大作业,下载即用无需修改,项目完整确保可以运行。 基于risc-V的五级流水线CPU实验项目源码+文档说明.zip已获导师指导并通过的97分的高分课程
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB取整函数与Web开发的作用:round、fix、floor、ceil在Web开发中的应用

![MATLAB取整函数与Web开发的作用:round、fix、floor、ceil在Web开发中的应用](https://img-blog.csdnimg.cn/2020050917173284.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2thbmdqaWVsZWFybmluZw==,size_16,color_FFFFFF,t_70) # 1. MATLAB取整函数概述** MATLAB取整函数是一组强大的工具,用于对数值进行
recommend-type

我想做python的算法工程师,我应该学什么?学习的顺序是什么?网上有什么推荐的免费课程吗?回答具体精确一点不要太笼统

对于想要成为 Python 算法工程师的人来说,他们应该先从掌握 Python 的基础语法开始,然后学习数据结构和算法,并且要深入了解 Python 中的一些科学计算和数据处理库,比如 NumPy、Pandas 等。 学习的顺序可以是先学习基础语法和编程技巧,然后再学习数据结构和算法相关的知识,最后深入了解数据处理和科学计算相关的库。 对于免费课程的推荐,我建议你可以先去 Coursera、edX、Udacity 等网站上寻找相关课程,这些网站上有很多优质的 Python 编程和算法课程,你可以根据自己的需求和学习进度进行选择。此外,还可以考虑一些在线编程网站,如 HackerRank、L
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。