我想解析一个.ubx文件(=我的输入文件)。此文件包含许多不同的NMEA语句以及原始接收器数据。输出文件应该只包含GGA语句的信息。只要.ubx文件不包含任何原始消息,这就可以正常工作。但是如果它包含原始数据
我得到以下错误:
回溯(最近一次呼叫):
文件“C:。。。myParser.py“,第25行,英寸
对于行读取器:
错误:行包含空字节
我的代码如下:import csv
from datetime import datetime
import math
# adapt this to your file
INPUT_FILENAME = 'Rover.ubx'
OUTPUT_FILENAME = 'out2.csv'
# open the input file in read mode
with open(INPUT_FILENAME, 'r') as input_file:
# open the output file in write mode
with open(OUTPUT_FILENAME, 'wt') as output_file:
# create a csv reader object from the input file (nmea files are basically csv)
reader = csv.reader(input_file)
# create a csv writer object for the output file
writer = csv.writer(output_file, delimiter=',', lineterminator='\n')
# write the header line to the csv file
writer.writerow(['Time','Longitude','Latitude','Altitude','Quality','Number of Sat.','HDOP','Geoid seperation','diffAge'])
# iterate over all the rows in the nmea file
for row in reader:
if row[0].startswith('$GNGGA'):
time = row[1]
# merge the time and date columns into one Python datetime object (usually more convenient than having both separately)
date_and_time = datetime.strptime(time, '%H%M%S.%f')
date_and_time = date_and_time.strftime('%H:%M:%S.%f')[:-6] #
writer.writerow([date_and_time])
我的.ubx文件如下所示:
^{pr2}$
我已经找过类似的问题了。然而,我没能找到一个对我有用的解决办法。在
最后我得到了这样的代码:import csv
def unfussy_reader(csv_reader):
while True:
try:
yield next(csv_reader)
except csv.Error:
# log the problem or whatever
print("Problem with some row")
continue
if __name__ == '__main__':
#
# Generate malformed csv file for
# demonstration purposes
#
with open("temp.csv", "w") as fout:
fout.write("abc,def\nghi\x00,klm\n123,456")
#
# Open the malformed file for reading, fire up a
# conventional CSV reader over it, wrap that reader
# in our "unfussy" generator and enumerate over that
# generator.
#
with open("Rover.ubx") as fin:
reader = unfussy_reader(csv.reader(fin))
for n, row in enumerate(reader):
fout.write(row[0])
但是,我不能简单地使用上面的代码编写一个包含unfuss_阅读器包装器读入的所有行的文件。在
如果你能帮助我,我会很高兴的。在
下面是.ubx文件在记事本+image中的外观图像
谢谢!在