Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 使用xlsxwriter将Python34 NMEA语句放入xlsx_Arrays_Excel_Python 3.x_Xlsxwriter_Nmea - Fatal编程技术网

Arrays 使用xlsxwriter将Python34 NMEA语句放入xlsx

Arrays 使用xlsxwriter将Python34 NMEA语句放入xlsx,arrays,excel,python-3.x,xlsxwriter,nmea,Arrays,Excel,Python 3.x,Xlsxwriter,Nmea,我正在做一个nmea句子项目,在这个项目中,我得到一个txt文件,其中包含由逗号分隔的nmea句子。我正在尝试使用xlsxwriter将结果写入excel。我想要的结果是将时间戳、纬度、经度和海拔打印成相互对应的行……但我总是在将其他迭代写入xlsx之前将“查询”的第一次迭代写入五次。我知道一定有一个简单的解决办法。你能告诉我哪里出了问题吗 这是我的剧本的开始,所以你对正在发生的事情有一个完整的了解 import os import csv from csv import * import n

我正在做一个nmea句子项目,在这个项目中,我得到一个txt文件,其中包含由逗号分隔的nmea句子。我正在尝试使用xlsxwriter将结果写入excel。我想要的结果是将时间戳、纬度、经度和海拔打印成相互对应的行……但我总是在将其他迭代写入xlsx之前将“查询”的第一次迭代写入五次。我知道一定有一个简单的解决办法。你能告诉我哪里出了问题吗


这是我的剧本的开始,所以你对正在发生的事情有一个完整的了解

import os
import csv
from csv import *
import numpy
import matplotlib
from numpy import *
from matplotlib import *
import matplotlib.pyplot as plt
from matplotlib.pylab import *
import numpy as np


#to export to excel
import xlsxwriter
from xlsxwriter.workbook import Workbook

#to get the csv converter functions
import os
import subprocess
import glob

#to get the datetime functions
import datetime
from datetime import datetime
from pytz import timezone
import time
import calendar


#creates the path needed for incoming and outgoing files
path_in = 'C:/Python34/gps_txts/'
path_out = 'C:/Python34/output_files/'

#prints all the data in the file if you want
q_show_content = input('Print list of files type y:')
if q_show_content == 'y':
    for root, dirs, files in os.walk(path_in):
          print(root, dirs, files)
else:
    print('ok')


data = []  #empty because we will store data into it


#Reads a CSV file and return it as a list of rows
def read_csv_file(filename):
    """Reads a CSV file and return it as a list of rows."""

    for row in csv.reader(open(filename)):
        data.append(row)
    return data

#request of what file to look at
print ("- - - - - - - - - - - - -")
data_file = input('Which file do you want to look at?')

f = open(path_in + data_file)
read_it = read_csv_file(path_in + data_file)

with f as csvfile:
    readCSV = csv.reader(csvfile,delimiter=',')
    plots = csv.reader(csvfile, delimiter=',')


#creates the workbook
output_filename = input('output filename:')
workbook = xlsxwriter.Workbook(path_out + output_filename + '.xlsx')
worksheet = workbook.add_worksheet()

#formatting definitions
bold    = workbook.add_format({'bold': True})
date_format = workbook.add_format({'num_format': "m/d/yyyy hh:mm:ss"})

#print number of rows
print ("- - - - - - - - - - - - -")
rows = len(read_it)
print (data_file, " has "+ str(rows) + " rows of data")
print ("- - - - - - - - - - - - -")

#Counts the number of times a GPS command is observed
def list_gps_commands(data):
    """Counts the number of times a GPS command is observed.

Returns a dictionary object."""

    gps_cmds = dict()
    for row in data:
        try:
            gps_cmds[row[0]] += 1 
        except KeyError:
            gps_cmds[row[0]] = 1

    return gps_cmds

print(list_gps_commands(read_it))
print ("- - - - - - - - - - - - -")


#prints all the data in the file if you want

q_show_data = input('Print data inside ' + data_file + ' type y:')
if q_show_data == 'y':
    for row in read_it:
        print(row)
else:
    print('ok')

这是我有意见的部分

#Function process_gps_data for GPS 

NMI = 1852.0
def process_gps_data(data):
    """Processes GPS data, NMEA 0183 format.

Returns a tuple of arrays: latitude, longitude, velocity [km/h],
time [sec] and number of satellites.
See also: http://www.gpsinformation.org/dale/nmea.htm.
    """


    latitude  = []
    longitude = []
    altitude  = []
    velocity  = []
    timestamp = []
    num_sats  = []



    for row in data:

        if row[0] == '$GPRMC':     # Valid position/time sentence
            y = (float(row[3][0:2]) + float(row[3][2:])/60.0)
            if row[4] == "S":
                y = -y
            latitude.append(y)
            x = (float(row[5][0:3]) + float(row[5][3:])/60.0)
            if row[6] == "W":
                x = -x
            longitude.append(x)
            print('x,y:',x,y)
            velocity.append(float(row[7])*NMI/1000.0)


            gpstime = row[1][0:6]                     # hhmmss
            gdate = row[9]                            # ddmmyy
            gpsdate = gdate[4:6]+gdate[2:4]+gdate[0:2]  # yymmdd

            real_time =gpsdate + gpstime
            add_date_time = datetime.strptime(real_time, "%y%m%d%H%M%S")
            print(add_date_time)

            timestamp.append(add_date_time)



    return (array(latitude), array(longitude), array(velocity), array(timestamp))





# how to call process_gps_data()
(lati, long, v, t_stamp) = process_gps_data(data)


print ("- - - - - - - - - - - - -")
print('lati:',lati)
print ("- - - - - - - - - - - - -")
print('long:',long)
print ("- - - - - - - - - - - - -")
print('v:',v)
print ("- - - - - - - - - - - - -")
print('date:', t_stamp)
print ("- - - - - - - - - - - - -")





#sets up the header row
worksheet.write('A1','TimeStamp',bold)
worksheet.write('B1', 'Latitude',bold)
worksheet.write('C1', 'Longitude',bold)
worksheet.write('D1', 'Velocity',bold)
worksheet.autofilter('A1:D1')   #dropdown menu created for filtering

# Create a For loop to iterate through each row in the XLS file, starting at row 2 to skip the headers
for r, row in enumerate(data, start=1):  #where you want to start printing results inside workbook
    for c, col in enumerate(row):
        worksheet.write_column(r,0, t_stamp, date_format)
        worksheet.write_column(r,1, lati)
        worksheet.write_column(r,2, long)
        worksheet.write_column(r,3, v)


workbook.close()
f.close()

我正在使用一个虚拟gps缓存,在txt文件中包含这些行。请注意,GPRMC在第[9]行中有不同的年份==01,02,03

$GPRMC,002454,A,3553.5295,N,13938.6570,E,0.0,43.1,180701,7.1,W,A*3F
$GPRMB,A,,,,,,,,,,,,A,A*0B
$GPGGA,002455,3553.5295,N,13938.6570,E,1,05,2.2,18.3,M,39.0,M,,*7F
$GPRMC,002456,A,3553.5321,N,13938.6581,E,0.0,43.1,180702,7.1,W,A*3D
$GPRMC,104715.20,A,5100.2111,N,00500.0006,E,21.7,003.0,140803,01.,W*70
我的结果被打印出来,看起来就像他们期望的那样

- - - - - - - - - - - - -
garmin etrex summit.txt  has 5 rows of data
- - - - - - - - - - - - -
{'$GPRMC': 3, '$GPGGA': 1, '$GPRMB': 1}
- - - - - - - - - - - - -
Print data inside garmin etrex summit.txt type y:
ok
x,y: 139.64428333333333 35.892158333333334
2000-07-18 00:24:54
x,y: 139.64430166666668 35.892201666666665
2001-07-18 00:24:56
x,y: 5.00001 51.00351833333333
2003-08-14 10:47:15
- - - - - - - - - - - - -
lati: [ 35.89215833  35.89220167  51.00351833]
- - - - - - - - - - - - -
long: [ 139.64428333  139.64430167    5.00001   ]
- - - - - - - - - - - - -
v: [  0.       0.      40.1884]
- - - - - - - - - - - - -
date: [datetime.datetime(2000, 7, 18, 0, 24, 54)
 datetime.datetime(2001, 7, 18, 0, 24, 56)
 datetime.datetime(2003, 8, 14, 10, 47, 15)]
- - - - - - - - - - - - -
但当我查看生成的xlsx文件时,前五行如下所示:

TimeStamp             Latitude   Longitude  Velocity        
7/18/2001 00:24:54  35.89215833 139.6442833   0
7/18/2001 00:24:54  35.89215833 139.6442833   0
7/18/2001 00:24:54  35.89215833 139.6442833   0
7/18/2001 00:24:54  35.89215833 139.6442833   0
7/18/2001 00:24:54  35.89215833 139.6442833   0
7/18/2002 00:24:56  35.89220167 139.6443017   0
8/14/2003 10:47:15  51.00351833 5.00001      40.1884
所以我的问题是,我得到了5个相同的第一个“查询”,然后是另外2个“$GPRMC”迭代


我哪里出错了?

应该
读取它中的行:
读取数据中的行:
?@xidgel我正在使用脚本前面的数据将txt文件中的字符串作为csv文件读取。我创建变量read_it是为了读取csv文件中的数据。我对python相当陌生,并且在学习的过程中不断学习。我想看看是否有一种简单的方法可以查看以“$GPRMC”开头的每一行,然后使用xlsxwriter将与该行对应的某些“字段”打印到自己指定的行中,并将其打印到xlsx文件中。这可能吗?我编辑了我的问题并反映了数据中的for行:read_it:be
for row in data:
?@xidgel我在脚本前面使用数据将txt文件中的字符串作为csv文件读取。我创建变量read_it是为了读取csv文件中的数据。我对python相当陌生,并且在学习的过程中不断学习。我想看看是否有一种简单的方法可以查看以“$GPRMC”开头的每一行,然后使用xlsxwriter将与该行对应的某些“字段”打印到自己指定的行中,并将其打印到xlsx文件中。这可能吗?我编辑了我的问题,并在数据中反映了for行: