Python Strings formatting

This is the place for queries that don't fit in any of the other categories.

Python Strings formatting

Postby suga » Thu Jun 12, 2014 10:49 am

I have the following input file :
berlin t 0 airline
one n 0 flightnumber
one n 0 flightnumber
turn t com turn_heading
right t 0 direction
heading t com turn_heading

I have written a code:
Code: Select all
from collections import OrderedDict
output = OrderedDict()
a = ['reduce' , 'descend' , 'turn_heading' , 'cleared_ils' , 'handover' , 'heading' , 'give_speed']
b = ['degree_absolute' , 'speed' , 'flightlevel' , 'altitude' , 'runway' , 'contact']
c = ['more']
#tag1 = ""
with open('out6') as in_file:
    for line in in_file:   
   columns = line.split('   ')
      if len(columns) >= 2:
      word, f1, f2, tag = line.strip().split()
      if tag in a:
         tag = 'command = ' +  tag
      elif tag in b:
         tag = 'subclass = ' + tag
      elif tag in c:
         tag = 'subclass1 = ' + tag
      if output.has_key(tag) == False:
         output[tag] = [];
      output[tag].append(word)
   else:
      print ""
      for k, v in output.items():
             print '<{}> {} </{}>'.format(k, ' '.join(v), k)
      output = OrderedDict()


My current output is :
Code: Select all
<airline> berlin <airline>
<flightnumber> one one <flightnumber>
<turn_heading> turn heading <turn_heading>
<direction> direction <direction>

My expected output :
Code: Select all
<airline> berlin </airline>
<flightnumber> one one </flightnumber>
<command = turn_heading> turn 
<direction> direction </direction>
heading  </command = turn_heading>

Can Someone help me in getting this .. what changes should I do ???
Last edited by Yoriz on Thu Jun 12, 2014 12:02 pm, edited 1 time in total.
Reason: First post lock.
suga
 
Posts: 1
Joined: Thu Jun 12, 2014 10:39 am

Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], snippsat and 5 guests