0

I have a text file that looks like the following:

AELSLAEM A3LYKM

AELSLAEM A3LYKM WRH7MT3 AELLHH WBRKAETHH

HHL TSMH7 LY

LW SMH7T

MN FD9LK

HHL TTKRM A3LY B

MN FD9LK AHRYD

AHNAE AHRYD MN FD9LK

MN FD9LK AHH7D9R LY

HHL LDYK H7JHZ

AENTDH6R QLYLAE

SAHNTDH6R HHNAE

MTAE YGHLQ AELMT6A3M

MTAE YFTH7 AELMT6A3M ...

I would like to add a white space after each of these characters:

characters = [AH, AE2, UW, EH, AE, B, T3, T, TH, JH, H7, X, D, DH, R, Z, S, SH, S9, D9, T6, DH6, A3, GH, F, Q, K, L, M, N, HH, W, Y, AA, AN, IH, EN, UN, 2]

so the output would look like the following:

AE L S L AE M A3 L Y K M

AE L S L AE M A3 L Y K M W R H7 M T3 AE L L HH W B R K AE T HH

HH L T S M H7 L Y

L W S M H7 T

M N F D9 L K

HH L T T K R M A3 L Y B

M N F D9 L K AH R Y D

What I am mostly having trouble with is adding space, for example, after D only if it is followed by a digit or H.. I would like to do this in python or (if possible) vim. Could any one help with this. thank you!

Community
  • 1
  • 1
Aloush87
  • 15
  • 1
  • 7
  • try this, `for each in characters: if each in contents: contents = contents.replace(each, each + " ")` – Jaimin Ajmeri Nov 29 '16 at 04:05
  • Thank you Jaimin.. here is the script.. I am getting an error! 'inp = open("input.txt", "r") inp1 = inp.readline() characters = ['AH', 'AE2', 'UW', 'EH', 'AE', 'B', 'T3', 'T', 'TH', 'JH', 'H7', 'X', 'D', 'DH', 'R', 'Z', 'S', 'SH', 'S9', 'D9', 'T6', 'DH6', 'A3', 'GH', 'F', 'Q', 'K', 'L', 'M', 'N', 'HH', 'W', 'Y', 'AA', 'AN', 'IH', 'EN', 'UN', '2'] for each in characters: { if each in inp1: inp1= inp1.replace(each, each + " ") #inp1= inp1.replace(chars, chars + ' ') #print inp1 }' – Aloush87 Nov 29 '16 at 05:03
  • What is the error ? – Jaimin Ajmeri Nov 29 '16 at 05:09
  • `{` and `}` this shouldn't be there, are you trying to say that your getting error within the parenthesis ? Use `inp.read() ` instead of `inp.readline() ` – Jaimin Ajmeri Nov 29 '16 at 05:15
  • Thank you! it finally worked! I am however not getting output! I added a print statement in the loop: 'print content' but that gave an error! – Aloush87 Nov 29 '16 at 05:49
  • I have post the script as answer. I cannot tell you where you are getting it wrong unless you tell me the exact error you are getting. – Jaimin Ajmeri Nov 29 '16 at 05:53

2 Answers2

0

You can try using the replace method. So you go through the character array using a for each loop (or regular for loop if you prefer) and say replace all instances of this character with the character plus a space:

for chars in characters:
{
    str = str.replace(chars, chars + ' ')
}

That's assuming you read in the file into a variable called str.

0

This works for me,

import sys

try:
    inp = open("input.txt", "r") 
    inp1 = inp.read()
    inp.close()
except Exception, e:
    print "File error:", e
    sys.exit(-1)

print "\nFile Contents:\n", inp1
characters = ['AH', 'AE2', 'UW', 'EH', 'AE', 'B', 'T3', 'T', 'TH', 'JH', 'H7', 'X', 'D', 'DH', 'R', 'Z', 'S', 'SH', 'S9', 'D9', 'T6', 'DH6', 'A3', 'GH', 'F', 'Q', 'K', 'L', 'M', 'N', 'HH', 'W', 'Y', 'AA', 'AN', 'IH', 'EN', 'UN', '2'] 
for each in characters: 
    if each in inp1: 
        inp1= inp1.replace(each, each + " ") 

print "\nOutput:\n", inp1
Jaimin Ajmeri
  • 572
  • 3
  • 18
  • It does not like the line 7 _except Exception, e:_ I get invalid syntax error **except Exception, e** – Aloush87 Nov 29 '16 at 15:45
  • When I use 'as e' instead, I get this error _Syntax error:missing parentheses in call to print_ in the next line: **print "File error:", e** – Aloush87 Nov 29 '16 at 15:56
  • @AliAlshehri thats because you are using python 3, I guess. See [here](http://stackoverflow.com/questions/25445439/what-does-syntaxerror-missing-parentheses-in-call-to-print-mean-in-python) – Jaimin Ajmeri Nov 30 '16 at 02:55