python文本文件读取失败,Python读入文件:错误:行包含NULL by

博主正在尝试从.ubx文件中解析GPS NMEA语句,特别是GGA语句,并将结果写入CSV文件。遇到的问题是当文件包含原始接收器数据时,代码会因空字节错误而崩溃。已尝试使用unfussy_reader函数来处理,但无法正确写入所有行到输出文件。寻求帮助以解决这个问题。
摘要由CSDN通过智能技术生成

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

谢谢!在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值