Python读入文件:错误:行包含NULL字节

时间:2016-07-04 07:55:19

标签: python parsing nmea

我想解析.ubx文件(=我的输入文件)。该文件包含许多不同的NMEA语句以及原始接收者数据。输出文件应该只包含GGA句子中的信息。只要.ubx文件不包含任何原始消息,这样就可以正常工作。但是,如果它包含原始数据 我收到以下错误:

追踪(最近一次通话):   文件“C:... myParser.py”,第25行,in     读者行: 错误:行包含NULL字节

我的代码如下所示:

    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文件如下所示:

    $GNGSA,A,3,16,25,29,20,31,26,05,21,,,,,1.30,0.70,1.10*10
$GNGSA,A,3,88,79,78,81,82,80,72,,,,,,1.30,0.70,1.10*16
$GPGSV,4,1,13,02,08,040,17,04,,,47,05,18,071,44,09,02,348,24*49
$GPGSV,4,2,13,12,03,118,24,16,12,298,36,20,15,118,30,21,44,179,51*74
$GPGSV,4,3,13,23,06,324,35,25,37,121,47,26,40,299,48,29,60,061,49*73
$GPGSV,4,4,13,31,52,239,51*42
$GLGSV,3,1,10,65,07,076,24,70,01,085,,71,04,342,34,72,13,029,35*64
$GLGSV,3,2,10,78,35,164,41,79,75,214,48,80,34,322,46,81,79,269,49*64
$GLGSV,3,3,10,82,28,235,52,88,39,043,43*6D
$GNGLL,4951.69412,N,00839.03672,E,124610.00,A,D*71
$GNGST,124610.00,12,,,,0.010,0.010,0.010*4B
$GNZDA,124610.00,03,07,2016,00,00*79
µb<    ¸½¸Abð½                  .        SB  éF  é v.¥  #  1   f  =•Iè    ,
Ïÿÿ£Ëÿÿd¡ ¬M    0+ùÿÿ³øÿÿµj    #ª    ² -K*  
   ,¨  ,  éºJU     /)  ++  f  5    .lG  NL  C8G     /{;  „>  é óK       3  —  Bòl  .   "¿  2      bm¡  
  4âH  ÐM  X cRˆ     35  »7  Óo‡ž    "*ßÿÿØÜÿÿUhQ`  
   3ŒðÿÿÂïÿÿþþûù     ÂÈÿÿñÅÿÿJX ES
     $²I  uM  N:w    (YÃÿÿV¿ÿÿ> =ìî     1¥éÿÿèÿÿmk³m     /?ÔÿÿÒÿÿšz+Ú     ­Ïÿÿ6ÍÿÿêwÇ\  ?   ]?  ˜B  Aÿƒ    y µbÐD‹lçtæ@p3,}ßœŒ-vAh
    ¿M"A‚UE   ôû JQý
'wA´üát¸jžAÀ‚"Å     
 )DÂï–ŽtAöÙüñÅ›A|$Å   ôû/ Ìcd§ÇørA†áãì˜AØY–Ä   ôû1 /Áƒ´zsAc5+_’ô™AìéNÅ  ôû( ¶y(,wvAFøÈV§ƒA˜ÝwE   ôû$ _S R‰wAhÙ]‘ÑëžAÇ9Å      vwAòܧsAŒöƒd§Ò™AÜOÄ   ôû3 kœÕ}vA;D.ž‡žAÒûàÄ @ˆ" ϬŸ   ntAfˆÞ3ךA~Y2E   ôû3 :GVtAæ93l)ÆšAß yE ôû4 Uþy.TwA<âƒ' ¦žAhmëC   ôû" ¯4Çï ›wAþ‰Ì½6ŸAŠû¶D          ~~xI]tA<ÞÿrÁšAmHE   ôû/ ÖÆ@ÈgŸsAXnþ‚†4šA'0tE ôû. ·ÈO:’
sA¢B†i™Aë%
E   ôû/ >Þ,À8vA°‚9êœA>ÇD   ôû, ø(¼+çŠuAÆOÁ לAÈΆD 
ôû# ¨Ä-_c¯qAuÓ?]> —AÐкà ôû0 ÆUV¨ØZsA]ðÛñß™AÛ'Å     ôû, ™mv7žqAYÐ:›Ä‘—AdWxD ôû1 ûö>%vA}„
ëV˜A.êbE   

 AÝ$GNRMC,124611.00,A,4951.69413,N,00839.03672,E,0.009,,030716,,,D*62
$GNVTG,,T,,M,0.009,N,0.016,K,D*36
$GNGNS,124611.00,4951.69413,N,00839.03672,E,RR,15,0.70,162.5,47.6,1.0,0000*42
$GNGGA,124611.00,4951.69413,N,00839.03672,E,4,12,0.70,162.5,M,47.6,M,1.0,0000*6A
$GNGSA,A,3,16,25,29,20,31,26,05,21,,,,,1.31,0.70,1.10*11
$GNGSA,A,3,88,79,78,81,82,80,72,,,,,,1.31,0.70,1.10*17
$GPGSV,4,1,13,02,08,040,18,04,,,47,05,18,071,44,09,02,348,21*43
$GPGSV,4,2,13,12,03,118,24,16,

我已经搜索过类似的问题了。但是我无法找到适合我的解决方案。

我最终得到了这样的代码:

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_reader包装器读入的所有行的文件。

如果你能帮助我,我会很高兴的。

以下是.ubx文件在notepad ++ image

中的外观图

谢谢!

3 个答案:

答案 0 :(得分:0)

我不太确定,但你的文件看起来很漂亮。您应该尝试打开它

with open(INPUT_FILENAME, 'rb') as input_file:

答案 1 :(得分:0)

好像你没有用正确的编码格式打开文件。 因此无法正确读取原始消息。

如果编码为UTF8,则需要使用编码选项打开文件:

with open(INPUT_FILENAME, 'r', newline='', encoding='utf8') as input_file

答案 2 :(得分:0)

嘿,如果有其他人有这个proglem读取UBlox .ubx文件的NMEA句子 这个pyhton代码对我有用:

def read_in():
with open('GNGGA.txt', 'w') as GNGGA:
    with open('GNRMC.txt','w') as GNRMC:
        with open('rover.ubx', 'rb') as f:
            for line in f:
                #print line
                if line.startswith('$GNGGA'):
                    #print line
                    GNGGA.write(line)
                if line.startswith('$GNRMC'):
                    GNRMC.write(line)

read_in()