how to take the specific details out in Python that are separated by a semi colon or a slash?

wasima

I have the following results from a vet analyser

result{type:PT/APTT;error:0;PT:32.3 s;INR:0.0;APTT:119.2;code:470433200;lot:405 4H0401;date:20/01/2017 06:47;PID:TREKKER20;index:015;C1:-0.1;C2:-0.1;qclock:0;ta rget:2;name:;Sex:;BirthDate:;operatorID:;SN:024000G0900046;version:V2.8.0.09}

Using Python how do i separate the date the time the type PT and APTT.... please note that the results will be different everytime so i need to make a code that will find the date using the / and will get the time because of four digits and the : .... do i use a for loop?

Bojan P.

This code makes further usage of fields easier by converting them to dict.

from pprint import pprint

result = "result{type:PT/APTT;error:0;PT:32.3 s;INR:0.0;APTT:119.2;code:470433200;lot:405 4H0401;date:20/01/2017 06:47;PID:TREKKER20;index:015;C1:-0.1;C2:-0.1;qclock:0;ta rget:2;name:;Sex:;BirthDate:;operatorID:;SN:024000G0900046;version:V2.8.0.09}"

if result.startswith("result{") and result.endswith("}"):
    result = result[(result.index("{") + 1):result.index("}")]
# else:
#    raise ValueError("Invalid data '" + result + "'")

# Separate fields
fields = result.split(";")
# Separate field names and values
# First part is the name of the field for sure, but any additional ":" must not be split, as example "date:dd/mm/yyyy HH:MM" -> "date": "dd/mm/yyyy HH:MM"
fields = [field.split(":", 1) for field in fields]
fields = {field[0]: field[1] for field in fields}

a = fields['type'].split("/")

print(fields)
pprint(fields)
print(a)

The result:

{'type': 'PT/APTT', 'error': '0', 'PT': '32.3 s', 'INR': '0.0', 'APTT': '119.2', 'code': '470433200', 'lot': '405 4H0401', 'date': '20/01/2017 06:47', 'PID': 'TREKKER20', 'index': '015', 'C1': '-0.1', 'C2': '-0.1', 'qclock': '0', 'ta rget': '2', 'name': '', 'Sex': '', 'BirthDate': '', 'operatorID': '', 'SN': '024000G0900046', 'version': 'V2.8.0.09'}
{'APTT': '119.2',
 'BirthDate': '',
 'C1': '-0.1',
 'C2': '-0.1',
 'INR': '0.0',
 'PID': 'TREKKER20',
 'PT': '32.3 s',
 'SN': '024000G0900046',
 'Sex': '',
 'code': '470433200',
 'date': '20/01/2017 06:47',
 'error': '0',
 'index': '015',
 'lot': '405 4H0401',
 'name': '',
 'operatorID': '',
 'qclock': '0',
 'ta rget': '2',
 'type': 'PT/APTT',
 'version': 'V2.8.0.09'}
['PT', 'APTT']

Note that dictionaries are not sorted (they don't need to be in most cases as you access the fields by the keys).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to read a file with a semi colon separator in pandas

Grouping the details of the data into one, price field should be separated by semi-colon

CustomActionData with semi-colon separated, causes string overflow - What are the common workaround to this solution?

Javascript: Get first number substring for each semi-colon separated substring

How to store colon separated strings in hashtable?

How to semi_join two dataframes by string column with one being colon-separated

How do I calculate semi-colon separated values within a cell?

How to extract a specific column from : ( colon ) separated values?

How do I filter rows depending on values in semi-colon separated columns?

How to take out a word from a list which is separated by ":"?

How do I save an excel spreadsheet as a semi-colon separated values file?

How to escape colon and double slash in shell script?

Convert cURL multi-line output to single, semi-colon-separated line

how does the Semi Colon work while using out.write?

how to take out the mean from python matrix

Get column values separated by semi colon

Node separate the lines in data separated by a semi-colon

How to take an input of line separated integers as a list in python

How to create JSON from a slash separated String?

PowerShell Filter from Array based on Email separated by semi-colon, Object value in the array separated by pipe

Join lines separated by a colon in Python

Regex for one element or semi-colon separated list of max. 2 elements

Python parse nested JSON file and take out specific attributes

python: parse a colon-separated formatted string

How to change a characters separated by a colon into an integer?

How take out a Specific element from a list?

How to replace commas with semi-colon except commas in quotes Apache beam python

Excel - How to split semi-colon separated values in multiple columns into rows

How Can I Use Data Validation On Single or Multiple Email Addresses That are Semi-colon Separated?