项目概述:

用户通过浏览器选择一个文件来导入数据。然后,我解包二进制文件。然后,我将新解包的数据保存为.csv文件,以便以后在excel中查看数据。用户当前通过键入新文件名来创建文件名。然后我继续用matplotlib库绘制所有数据。在

整个代码:#import Gnuplot

import struct

#import binascii

import csv

import matplotlib.pyplot as plt

import os.path

import pylab as pl

from Tkinter import Tk

from tkFileDialog import askopenfilename

#print "testing"

#data = '$'

#data2 = '7'

#out = ord(data)*256 + ord(data2)

#print out

'''

open_path = "/media/6265-D02D/"

fname = raw_input('Enter the file name you want to open (without the entire filepath)(include filetype ex. .txt): ')

#combines open_path with fname

entirepath = os.path.join(open_path, fname)

f = open(entirepath, 'r')

'''

#This opens the file browser inorder to the user to

#choose the file that they want to decipher

Tk().withdraw() '''we don't want a full GUI, so keep the root

window from appearing.

-This opens the filebrowser'''

filename1 = askopenfilename()

f = open(filename1 'r')

time = []

pitch = []

roll = []

yaw = []

p_rate = []

r_rate = []

y_rate = []

motor0 = []

motor1 = []

motor2 = []

motor3 = []

alt = []

thr_in = []

pitch_in = []

roll_in = []

yaw_in = []

byte_count = 50

s = struct.Struct('>3s I h h h h h h h h h h I h h h h s')

while True:

temp_data = []

unpacked_date = []

temp_data = f.read(40)

byte_count = len(temp_data)

if byte_count <40:

break

unpacked_data = s.unpack(temp_data)

if unpacked_data[0] == '$$$': #storing the unpacked data if a good packet is received

time.append(unpacked_data[1])

pitch.append(unpacked_data[2])

roll.append(unpacked_data[3])

yaw.append(unpacked_data[4])

p_rate.append(unpacked_data[5])

r_rate.append(unpacked_data[6])

y_rate.append(unpacked_data[7])

motor0.append(unpacked_data[8])

motor1.append(unpacked_data[9])

motor2.append(unpacked_data[10])

motor3.append(unpacked_data[11])

alt.append(unpacked_data[12])

thr_in.append(unpacked_data[13])

yaw_in.append(unpacked_data[14])

pitch_in.append(unpacked_data[15])

roll_in.append(unpacked_data[16])

#code not being used here

# Create plot from interpreted data

#pitch_plot = Gnuplot.Gnuplot()

#pitch_plot.title('Pitch vs. Time')

#pitch_plot('set style data linespoints')

#pitch_plot(time, pitch)

#creating a new csv file using the new parsed data

creatingcolumns = (time,pitch,roll,yaw, p_rate,

r_rate,y_rate,motor0,motor1,motor2,motor3,alt,

thr_in,yaw_in,pitch_in,roll_in)

creatingcolumns = zip(*creatingcolumns)

#creates headers

header = ["Time", "Pitch", "Roll", "Yaw", "P Rate",

"R Rate", "Y Rate", "Motor 0", "Motor 1", "Motor 2",

"Motor 3", "Altitude", "Thrust In", "Yaw In",

"Pitch in", "Roll in"]

#saves the file to the python, csv folder on the desktop

#determines the save path

save_path = "/home/pi/Desktop/Python CSV files"

#user inputs a file name

filename2 = raw_input("Give a name for the new CSV file (do not include the filetype such as .csv) ")

#combining everything into a variable for the save path

completeName = os.path.join(save_path, filename2+".csv")

#opens a new csvfile and writes the information to it

with open(completeName , "wb" ) as csvfile:

writingcsv = csv.writer(csvfile, dialect='excel')

writingcsv.writerow(header)

for row in creatingcolumns:

writingcsv.writerow(row)

#plots the data and creates the titles

#creates the first figure of Time vs Pitch

plt.figure(1)

plt.plot(time,pitch)

plt.xlabel('Time')

plt.ylabel('Pitch')

plt.title('Pitch vs Time')

fig = pl.gcf()

fig.canvas.set_window_title('Pitch vs Time')

#plt.show()

#creates second figure of Time vs Roll

plt.figure(2)

plt.plot(time, roll)

plt.title('Roll vs Time')

plt.xlabel('Time')

plt.ylabel('Roll')

fig2 = pl.gcf()

fig2.canvas.set_window_title('Roll vs Time')

#creates second figure of Time vs Yaw

plt.figure(3)

plt.plot(time, yaw)

plt.title('Yaw vs Time')

plt.xlabel('Time')

plt.ylabel('Yaw')

fig3 = pl.gcf()

fig3.canvas.set_window_title('Yaw vs Time')

#creates second figure of Time vs Pitch Rate

plt.figure(4)

plt.plot(time, p_rate)

plt.title('Pitch Rate vs Time')

plt.xlabel('Time')

plt.ylabel('Pitch Rate')

fig4 = pl.gcf()

fig4.canvas.set_window_title('Pitch Rate vs Time')

#creates second figure of Time vs Roll Rate

plt.figure(5)

plt.plot(time, r_rate)

plt.title('Roll Rate vs Time')

plt.xlabel('Time')

plt.ylabel('Roll Rate')

fig5 = pl.gcf()

fig5.canvas.set_window_title('Roll Rate vs Time')

#creates second figure of Time vs Yaw Rate

plt.figure(6)

plt.plot(time, y_rate)

plt.title('Yaw Rate vs Time')

plt.xlabel('Time')

plt.ylabel('Yaw Rate')

fig6 = pl.gcf()

fig6.canvas.set_window_title('Yaw Rate vs Time')

#creates second figure of Time vs Motor 0

plt.figure(7)

plt.plot(time, yaw)

plt.title('Motor 0 vs Time')

plt.xlabel('Time')

plt.ylabel('Motor 0')

fig7 = pl.gcf()

fig7.canvas.set_window_title('Motor 0 vs Time')

#creates second figure of Time vs Motor 1

plt.figure(8)

plt.plot(time, motor1)

plt.title('Motor 1 vs Time')

plt.xlabel('Time')

plt.ylabel('Motor 1')

fig8 = pl.gcf()

fig8.canvas.set_window_title('Motor 1 vs Time')

#creates second figure of Time vs Motor 2

plt.figure(9)

plt.plot(time, motor2)

plt.title('Motor 2 vs Time')

plt.xlabel('Time')

plt.ylabel('Motor 2')

fig9 = pl.gcf()

fig9.canvas.set_window_title('Motor 2 vs Time')

#creates second figure of Time vs Motor 3

plt.figure(10)

plt.plot(time, motor3)

plt.title('Motor 3 vs Time')

plt.xlabel('Time')

plt.ylabel('Motor 3')

fig10 = pl.gcf()

fig10.canvas.set_window_title('Motor 3 vs Time')

#creates second figure of Time vs Altitude

plt.figure(11)

plt.plot(time, alt)

plt.title('Altitude vs Time')

plt.xlabel('Time')

plt.ylabel('Altitude')

fig11 = pl.gcf()

fig11.canvas.set_window_title('Altitude vs Time')

#creates second figure of Time vs Thrust in

plt.figure(12)

plt.plot(time, thr_in)

plt.title('Thrust In vs Time')

plt.xlabel('Time')

plt.ylabel('Thrust In')

fig12 = pl.gcf()

fig12.canvas.set_window_title('Thrust In vs Time')

#creates second figure of Time vs Yaw Input

plt.figure(13)

plt.plot(time, yaw_in)

plt.title('Yaw Input vs Time')

plt.xlabel('Time')

plt.ylabel('Yaw Input')

fig13 = pl.gcf()

fig13.canvas.set_window_title('Yaw Input vs Time')

#creates second figure of Time vs Pitch Input

plt.figure(14)

plt.plot(time, pitch_in)

plt.title('Pitch Input vs Time')

plt.xlabel('Time')

plt.ylabel('Pitch Input')

fig14 = pl.gcf()

fig14.canvas.set_window_title('Pitch Input vs Time')

#creates second figure of Time vs Roll Input

plt.figure(15)

plt.plot(time, roll_in)

plt.title('Roll Input vs Time')

plt.xlabel('Time')

plt.ylabel('Roll Input')

fig15 = pl.gcf()

fig15.canvas.set_window_title('Roll Input vs Time')

plt.show()

我的问题:

我想在带有触摸屏的raspberry pi(Python2.7)上运行此代码。我想尽量避免打字。在

问题:

用户使用下面的代码选择要读取的文件。

这将创建一个类似于这样的变量:“C:/Users/Andrew/Desktop/Python/LOG1.txt”(注意:我意识到这是一个windows文件路径,在pi上它创建了一个linux文件路径)

^{pr2}$

以后我要在目录中输入一个新的文件名。在#saves the file to the python, csv folder on the desktop

#determines the save path

save_path = "/home/pi/Desktop/Python CSV files"

#user inputs a file name

filename2 = raw_input("Give a name for the new CSV file (do not include the filetype such as .csv) ")

#combining everything into a variable for the save path

completeName = os.path.join(save_path, filename2+".csv")

我想从变量filename1中去掉filepath的“LOG1”部分。所以基本上我删除了变量的“C:/Users/Andrew/Desktop/Python/”和“.txt”部分,但保留了文件名。然后,我希望使用这个文件名“LOG1”,并将其用于上面定义的completeName变量。我将使用以下代码:completeName = os.path.join(save_path,**INSERT VARIABLE HERE**, +".csv")

这将创建一个名为“LOG1.csv”的csv文件

如何删除文件路径的无关部分??

谢谢您!!在

python提取选中文件的文件名_如何从python文件路径中提取文件名?相关推荐

  1. python从文件路径中提取文件名、所在文件夹

    需求 有的时候我们想从文件路径中提取出包含扩展的文件名,有的时候需要不包括扩展的文件名,还有的时候想要获取文件所在目录,在python中如何快速实现呢? 实现 我们有如下路径 D:\Worksheet ...

  2. python批量读取文件名_python - 从路径中提取文件名,无论os / path形式如何

    python - 从路径中提取文件名,无论os / path形式如何 无论操作系统或路径格式是什么,我可以使用哪个Python库从路径中提取文件名? 例如,我想要所有这些路径返回c: a/b/c/ a ...

  3. python保存文件到桌面_我用Python不到一分钟就将桌面文件整理分类!

    大家好,又到了Python办公自动化专题 本文跟大家分享一个文件整理脚本的实现过程.具体的功能很简单,给定一个打算整理的文件夹目录,这个脚本可以将该目录下的所有文件都揪出来,并且根据后缀名归类到不同的 ...

  4. linux 脚本 提取文件名,powershell-从路径中提取文件名

    powershell-从路径中提取文件名 我想从以下路径中提取文件名: D:\Server\User\CUST\MEA\Data\In\Files\CORRECTED\CUST_MEAFile.csv ...

  5. Python语言学习:利用python获取当前/上级/上上级目录路径(获取路径下的最后叶目录的文件名、合并两个不同路径下图片文件名等目录/路径案例、正确加载图片路径)之详细攻略

    Python语言学习:利用python获取当前/上级/上上级目录路径(获取路径下的最后叶目录的文件名.合并两个不同路径下图片文件名等目录/路径案例.正确加载图片路径)之详细攻略 目录 利用python ...

  6. java 写文件 权限不够_教你解决Linux系统中JAVA创建文件后权限不足的问题

    在作业中,项目使用文件上传. 这个功能很常见. 当Kai Ge今天更改其官方帐户时,他遇到了一个问题,即无法访问下载的文件,也无法通过浏览器访问该文件. 它是怎么发生的? 经过许多问题之后,事实证明这 ...

  7. 如何从完整的文件路径中分离文件名和路径名?

    从路径中分离文件名: CString GetFileName(CString pathname)  {  for( int i=pathname.GetLength()-1; i>=0; i-- ...

  8. python提取文件指定列_如何从csv文件中提取特定列并使用python绘图

    我有一个csv文件,其中包含以下几行数据:# Vertex X Y Z K_I K_II K_III J 0 2.100000e+00 2.000000e+00 -1.000000e-04 0.000 ...

  9. python怎么读取pdf为文本_如何从pdf文件中提取特定文本python

    我试图摘录这段文字:DLA LAND AND MARITIME ACTIVE DEVICES DIVISION PO BOX 3990 COLUMBUS OH 43218-3990 USA Name: ...

  10. python怎么读取pdf为文本_轻松用Python批量提取PDF文本内容,这个小技巧告诉你!...

    轻松用Python批量提取PDF文本内容,这个小技巧告诉你!-1.jpg (22.73 KB, 下载次数: 0) 2018-9-7 08:33 上传 本文为你展示,如何用Python把许多PDF文件的 ...

最新文章

  1. 【机器学习PAI实战】—— 玩转人工智能之综述
  2. Java JDBC数据库 之 DBUtil 封装类
  3. Firefox不支持event解决方法
  4. as mysql with 嵌套_MySQL_MySQL的嵌套查询,MySQl从4.11版后已经完全支持嵌 - phpStudy
  5. 华为云 mysql 主备_安全性能两手抓,华为云MySQL“非双一特性”助力企业业务稳定高效运行...
  6. STM32-串口通信
  7. [河南省ACM省赛-第三届] AMAZING AUCTION (nyoj 251)
  8. java黄历_黄历查询API免费接口,黄历查询API接口付费定制-进制数据
  9. python编辑程序用print函数输出中国加油武汉加油_python练习1之print函数
  10. python生成字典脚本
  11. 一个简单的新闻发布系统
  12. PS CS6 打不开RAW格式文件
  13. Vim 文件浏览器(Netrw)
  14. 110 Ruby 版本管理器【Rails后端开发训练营】
  15. c++基础题:判断奇偶数
  16. scp过程中的两个问题解决 “The authenticity of host can‘t be established” “ Permission denied“
  17. python key lambda_python – 什么是key = lambda
  18. 【ELT.ZIP】OpenHarmony啃论文俱乐部——轻翻那些永垂不朽的诗篇
  19. 何为数据库连接池?其工作原理是什么?
  20. 笨办法学python练习35分支与函数

热门文章

  1. 人都说三十六计,我也来贡献一个版本
  2. 【软考-中级 网络工程师】第一章:计算机网络概论(思维导图/知识点)
  3. 软件项目管理的十大定律
  4. 学软件开发为什么要选 “猿代码任务制培训模式”?
  5. 设置电脑右下角显示秒钟
  6. duolinguo考试时摄像头/麦克风无法使用
  7. MATLAB中randi函数的用法
  8. c语言vc98打开路径,为什么找不到Microsoft Visual StudioVC98中的CRT文件夹?
  9. 【Python】芝麻HTTP代理系列保姆级全套攻略(对接教程+自动领取每日IP+IP最优算法)
  10. 等级保护----1、网络安全等级保护一级安全测评要求