Python读取文件:错误:行包含NULL by

2024-05-29 06:45:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我想解析一个.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中的外观图像

谢谢!在


Tags: and文件csvtheinfordatetime
3条回答

我不太确定,但你的文件看起来很二进制。你应该试着把它打开

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

似乎您没有用正确的编码格式打开文件。 因此无法正确读取原始消息。在

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

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

嘿,如果有其他人有这个程序在NMEA阅读uBlox.ubx文件的句子 普顿语对我有用:

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)

读入()

相关问题 更多 >

    热门问题