0

I have data in the following format:

Input Data:

#@ <id_1d3s2ia_p3m_zkjp59>
<Eckhard_Christian>     <hasGender>     <ma<>le> .
#@ <id_1jmz109_1gi_t71dyx>
<Peter_Pinn<>e>   <created>       <In_Your_Arms_(Love_song_from_"Neighbours")> .
#@ <id_v9bcjt_ice_fraki6>
<Blanchester,_Ohio>     <hasWebsite>    <http://www.blanchester.com/> .
#@ <id_10tunwc_p3m_zkjp59>
<Hub_(bassi~st)> <hasGender>     <ma??le> .

Output Data:

<Eckhard_Christian>     <hasGender>     <male> <id_1d3s2ia_p3m_zkjp59>.
<Peter_Pinne>   <created>       <In_Your_Arms_(Love_song_from_"Neighbours")> <id_1jmz109_1gi_t71dyx>.
<Blanchester,_Ohio>     <hasWebsite>    <http://www.blanchester.com/> <id_v9bcjt_ice_fraki6>.
<Hub_(bassist)> <hasGender>     <male> <id_10tunwc_p3m_zkjp59>.

That is in the output data I want to delete all other characters except: alphanumeric and :,/,/,.,_,(,) between any two starting and ending < >. I know python allows me to split using string.split() but if I split using < > as demarkers then for <ma<>le> I get (<ma,<>,le>).

Is there some other way by which I may split in python so that I may get the data in the desired form. Also I want preceding lines < > (following # @) to appear as the last column.

0

1 Answer 1

1

Assuming that there is always a whitespace character before/after the "proper" < and >, you could try something like this, using regular expressions:

import re
with open('data') as data:
    for line in data:
        if line.startswith('#@'):
            id_ = re.search('\s(<.*>)', line).group(1)
            fields = re.findall('(<.*?>)\s', next(data))
            fields = ['<' + re.sub(r'[^\w:/._()"]', '', f) + '>' for f in fields]
            print fields + [id_]

Output:

['<Eckhard_Christian>', '<hasGender>', '<male>', '<id_1d3s2ia_p3m_zkjp59>']
['<Peter_Pinne>', '<created>', '<In_Your_Arms_(Love_song_from_"Neighbours")>', '<id_1jmz109_1gi_t71dyx>']
['<Blanchester_Ohio>', '<hasWebsite>', '<http://www.blanchester.com/>', '<id_v9bcjt_ice_fraki6>']
['<Hub_(bassist)>', '<hasGender>', '<male>', '<id_10tunwc_p3m_zkjp59>']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.