By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,462 Members | 799 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,462 IT Pros & Developers. It's quick & easy.

normalize spaces in a txt file

P: 9
I want to write a function that takes a file and (corrects) deletes extra spaces after or before punctuations "() [] {} "" . , : ;! ? $" or add spaces where necessary.
example:
word .word should changes to word. word
word( word )word should changes to word (word) word

my code doesn't corrects the spaces!

Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. def c (file):
  4.     with open ('c.txt')as f1:
  5.         for line in f1:
  6.            result = re.sub(r'\s([?.!(){}[]:;,"](?:\s|$))', r'\1', line)
  7.            f2 = open ('zzzzz.txt', 'a')
  8.            f2.write(result)
  9.            print(result)
Mar 14 '15 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.