修复与地图最相似的特殊字符 [英] Fix special characters with closest equivalent without map

查看:50
本文介绍了修复与地图最相似的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Python中的传感器(GPS)获得输出,由于某种原因,输出并不完全干净.我已经在使用pynmea2及其校验和来过滤不良行,但是我想提高接受率.

I'm getting output from a sensor (GPS) in Python and for some reason, the output is not entirely clean. I'm already using pynmea2 and its checksum to filter out the bad rows but I want to improve the acceptance rate.

如果查看来自传感器的一些样本数据,则会发现许多字符已被人类可以纠正的字符替换,例如®=.或³=3.另一方面,有些不清楚,例如¶或±或Ç= G而不是C.

If you look at some sample data from the sensor, you see that many characters are replaced with something that could be corrected by a human, such as ® = . or ³ = 3. Some on the other hand, are less clear, such as ¶ or ± or Ç = G and not C.

我试图研究如何解决此问题,但是除了创建硬编码的地图或搜索并替换之外,我什么也没想出来.是否有图书馆或清理"垃圾邮件的方法?我的输入至少可以解决明显的问题,从而提高我的接受率?

I've tried to research how I could fix this but short of creating a hardcoded map or search and replace, I can't come up with anything. Is there a library or a way to "clean up" my input to solve at least the obvious one and thus boost my acceptance rate?

nmea = [
    "$GNRMC,175230.00,A,5231.08575,N,01324.94302,E,0.099,,300321,,,A,V*17",
    "$GNRMC,175211.00,A,5231.08495,N,01324.94370,E,2.771,348.30,300321,,,A,V*0F",
    "$GNGGA,175140.00,5231.06514,N,01325.03302,E,1,11,1.22,33.9,M,42.1,Í,,*7F",
    "$GNRMC,175141.00,A,5231.06307,N,01³25.02563,E,16.734,234.24,300³21,,,A,V*3A",
    "$GNGÇA,175141.00,5231.06307,N,01325.02563,E,1,11,1®22,33.6,M,42.1,M,,*75",
    "$GNRMC,175142.00,A,5231.°6059,N,01325.01869,E,17.220,235.29,300321,,,A,V*38",
    "$GNGGA,175142.00,5231.06059,N,01325.01869,E,1,11,±.22,33.5,M,42.1,M,,*79",
    "$GNRMC,175143.00,A,5231.05861,N,01325.01218,E,16.¶45,238.31,300321,,,A,V*37",
    "$GNGGA,175143.00,5231.05861,N,013²5.01218,E,1,11,1.22,34.7,M,42.1,M,,*71",
    "$GNRMC,175144.00,A,5231.05689,N,01325.00574,E,16.090,241.36,300321,,,A,V*33",
    "$GNGGA,175144.00,5231.05689,N,01325.00574,E,1,11,1.28,36.0,M,42.1,M,,*7D",
    "$GNRMC,175145.00,A,5231.05478,N,01324.99957,E,16.045,240.96,300321,,,A,V*31",
    "$GNGGA,175145.00,5231.05478,N,01324.99957,E,1,11,1.22,36.8,M,42.1,M,,*7E",
    "$GNRMC,175146®00,A,5231.05277,N,01324.99327,E,15.832,241.30,300321,,,A,V*30",
    "$GNGGA,175146.00,5231.05277,N,01324.99327,E,1,11,1.22,37.3,M,42.1,M,,*73",
    "$GNGGA,175230.00,5231.08575,N,01324.94302,E,1,12,0.96,56.7,M,42.1,M,,*7D"]

推荐答案

®/.³/3 Í/M (相同):

>>> for c in s:
...     print(f'{c} {ord(c):08b}')
... 
® 10101110
. 00101110
³ 10110011
3 00110011
Í 11001101
M 01001101

您的硬件可能有问题.由于数据是ASCII的,因此高位(位7)应该始终为0,因此,如果无法解决硬件问题,则可以过滤输出:

You likely have a problem with your hardware. Since the data is ASCII the high bit (bit 7) is supposed to always be a 0, so you could just filter the output if you can't fix the hardware issue:

import re
from pprint import pprint

nmea = [
    "$GNRMC,175230.00,A,5231.08575,N,01324.94302,E,0.099,,300321,,,A,V*17",
    "$GNRMC,175211.00,A,5231.08495,N,01324.94370,E,2.771,348.30,300321,,,A,V*0F",
    "$GNGGA,175140.00,5231.06514,N,01325.03302,E,1,11,1.22,33.9,M,42.1,Í,,*7F",
    "$GNRMC,175141.00,A,5231.06307,N,01³25.02563,E,16.734,234.24,300³21,,,A,V*3A",
    "$GNGÇA,175141.00,5231.06307,N,01325.02563,E,1,11,1®22,33.6,M,42.1,M,,*75",
    "$GNRMC,175142.00,A,5231.°6059,N,01325.01869,E,17.220,235.29,300321,,,A,V*38",
    "$GNGGA,175142.00,5231.06059,N,01325.01869,E,1,11,±.22,33.5,M,42.1,M,,*79",
    "$GNRMC,175143.00,A,5231.05861,N,01325.01218,E,16.¶45,238.31,300321,,,A,V*37",
    "$GNGGA,175143.00,5231.05861,N,013²5.01218,E,1,11,1.22,34.7,M,42.1,M,,*71",
    "$GNRMC,175144.00,A,5231.05689,N,01325.00574,E,16.090,241.36,300321,,,A,V*33",
    "$GNGGA,175144.00,5231.05689,N,01325.00574,E,1,11,1.28,36.0,M,42.1,M,,*7D",
    "$GNRMC,175145.00,A,5231.05478,N,01324.99957,E,16.045,240.96,300321,,,A,V*31",
    "$GNGGA,175145.00,5231.05478,N,01324.99957,E,1,11,1.22,36.8,M,42.1,M,,*7E",
    "$GNRMC,175146®00,A,5231.05277,N,01324.99327,E,15.832,241.30,300321,,,A,V*30",
    "$GNGGA,175146.00,5231.05277,N,01324.99327,E,1,11,1.22,37.3,M,42.1,M,,*73",
    "$GNGGA,175230.00,5231.08575,N,01324.94302,E,1,12,0.96,56.7,M,42.1,M,,*7D"]

def fix(m):
    a = m.group(0)
    b = chr(ord(m.group(0)) & 0x7F) # clear bit 7.
    print(f'Replaced {a} with {b}')
    return b

for i,v in enumerate(nmea):
    nmea[i] = re.sub(r'[\x80-\xff]',fix,v)

pprint(nmea)

输出:

Replaced Í with M
Replaced ³ with 3
Replaced ³ with 3
Replaced Ç with G
Replaced ® with .
Replaced ° with 0
Replaced ± with 1
Replaced ¶ with 6
Replaced ² with 2
Replaced ® with .
['$GNRMC,175230.00,A,5231.08575,N,01324.94302,E,0.099,,300321,,,A,V*17',
 '$GNRMC,175211.00,A,5231.08495,N,01324.94370,E,2.771,348.30,300321,,,A,V*0F',
 '$GNGGA,175140.00,5231.06514,N,01325.03302,E,1,11,1.22,33.9,M,42.1,M,,*7F',
 '$GNRMC,175141.00,A,5231.06307,N,01325.02563,E,16.734,234.24,300321,,,A,V*3A',
 '$GNGGA,175141.00,5231.06307,N,01325.02563,E,1,11,1.22,33.6,M,42.1,M,,*75',
 '$GNRMC,175142.00,A,5231.06059,N,01325.01869,E,17.220,235.29,300321,,,A,V*38',
 '$GNGGA,175142.00,5231.06059,N,01325.01869,E,1,11,1.22,33.5,M,42.1,M,,*79',
 '$GNRMC,175143.00,A,5231.05861,N,01325.01218,E,16.645,238.31,300321,,,A,V*37',
 '$GNGGA,175143.00,5231.05861,N,01325.01218,E,1,11,1.22,34.7,M,42.1,M,,*71',
 '$GNRMC,175144.00,A,5231.05689,N,01325.00574,E,16.090,241.36,300321,,,A,V*33',
 '$GNGGA,175144.00,5231.05689,N,01325.00574,E,1,11,1.28,36.0,M,42.1,M,,*7D',
 '$GNRMC,175145.00,A,5231.05478,N,01324.99957,E,16.045,240.96,300321,,,A,V*31',
 '$GNGGA,175145.00,5231.05478,N,01324.99957,E,1,11,1.22,36.8,M,42.1,M,,*7E',
 '$GNRMC,175146.00,A,5231.05277,N,01324.99327,E,15.832,241.30,300321,,,A,V*30',
 '$GNGGA,175146.00,5231.05277,N,01324.99327,E,1,11,1.22,37.3,M,42.1,M,,*73',
 '$GNGGA,175230.00,5231.08575,N,01324.94302,E,1,12,0.96,56.7,M,42.1,M,,*7D']

这篇关于修复与地图最相似的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆