Two str.split followed

0

I have a series of data ordered such that:

  

{"PseudorangeRateUncertaintyMetersPerSecond": 0.1289496429081,   "DriftUncertaintyNanosPerSecond": "", "AccumulatedDeltaRangeState": 1,   "ReceivedSvTimeNanos": 491721930367520.0, "TimeUncertaintyNanos": "",   "SnrInDb": "", "FullBiasNanos": -1.2125108815424e + 18, "State": 47,   "MultipathIndicator": "0", "AgcDb": "",   "PseudorangeRateMetersPerSecond": -80.016064827732, "TimeNanos":   40458000000, "Svid": 10, "AccumulatedDeltaRangeUncertaintyMeters":   0.002334289252758, "AccumulatedDeltaRangeMeters": -3022.5235041815, "BiasUncertaintyNanos": 5.3149323606571, "BiasNanos": 0,   "CarrierPhaseUncertainty": "", "CarrierFrequencyHz2": "\ n",   "TimeOffsetNanos": 0, "DriftNanosPerSecond": "", "CarrierFrequencyHz":   "", "ConstellationType": "1", "CarrierCycles": "",   "ReceivedSvTimeUncertaintyNanos": 25, "CarrierPhase": "",   "LeapSecond": "", "HardwareClockDiscontinuityCount": 0,   "ElapsedRealtimeMillis": 14068886, "Id": "Raw", "Cn0DbHz":   31.292695999146} {"GPS_time": "2018-06-08 16:35:24", "Altitude": 70.768542, "Longitude": -0.337611, "Provider": "gps", "Latitude": 39.479199, "Speed" : 0, "Id": "Fix", "Accuracy": 4} {"Number_of_sentences_for_full_data": 4, "Elevation_Degrees": 14,   "Azimuth_degrees": 62, "Sentence_1_of_2": 3,   "Number_of_satellites_in_view": 14, "Satellite_PRN_number": 21, "SNR":   "33", "CheckSum_data": 7, "Satellites_in_view": "$ GPGSV", "Id": "NMEA   GSV "} {" Latitude ": 3928.752338," HorizontalDilutionofPosition ": 0.4,   "TimeinSecondsSinceLastDGPSUpdate": "51.1", "Address_length": "W",   "GlobalPositioningSystemFixData": "$ GPGGA", "Direccion_Latitud": "N",   "FixQuality": 1, "DGPSStationIDNumber": "51.1", "Altitude": 19.6,   "Geoid_height_units": "M", "ChecksumData": "M",   "Fixtakenat_hh_mm_ss_UTC": 163508, "NumberofSatellitesTracked": 30,   "Geoid_height": 51.1, "Altitude_units": "M", "Id": "NMEA GGA",   "Length": 20.256369}

The program that I have separates me by "," but I also need to separate them by ":" if I can not access those values.

linea=r.readline()
if not linea:break
lect_linea=linea.split(',')
    
asked by Xabier Mikel 05.07.2018 в 10:29
source

1 answer

1

str.split returns a list with the different substrings resulting from dividing by the separator:

>>> linea = '"A": 0.12, "B": 2.5, "C": 1'
>>> linea.split(",")
['"A": 0.12', ' "B": 2.5', ' "C": 1']

To also divide by ":" you would only have to apply str.split on each item of the result list to apply split(",") iterating on it, for example using compression lists:

>>> linea = '"A": 0.12, "B": 2.5, "C": 1'
>>> r = [pareja.split(": ") for pareja in linea.split(", ")]
>>> r
[['"A"', '0.12'], ['"B"', '2.5'], ['"C"', '1']]

If you need to divide by several separators at the same time you can use re.split instead of str.split :

>>> import re
>>> linea = '"A": 0.12, "B": 2.5, "C": 1'
>>> res = re.split(r',\s|:\s', linea)
>>> res
['"A"', '0.12', '"B"', '2.5', '"C"', '1']

Since you are apparently trying to parse what is a valid Python dictionary or a valid JSON file, there are more efficient ways to do it:

  • Use ast.literal_eval (any valid representation of a Python dictionary):

    >>> from ast import literal_eval
    >>> texto = '{"A": 0.12, "B": 2.5, "C": 1}'
    >>> diccionario = literal_eval(texto)
    >>> diccionario
    {'A': 0.12, 'B': 2.5, 'C': 1}
    >>> diccionario["B"]
    2.5
    
  • Use json.loads (valid JSON):

    >>> import json
    >>> texto = '{"A": 0.12, "B": 2.5, "C": 1}'
    >>> diccionario = json.loads(texto)
    >>> diccionario
    {'A': 0.12, 'B': 2.5, 'C': 1}
    >>> diccionario["C"]
    1
    
answered by 05.07.2018 в 10:58