473,503 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

normalize spaces in a txt file

9 New Member
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
0 1321

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

Similar topics

1
8248
by: Desi | last post by:
XML gurus, I need help in converting my feedreader subscription XML file to an OPML file. I have no idea how to do that. Thanks in advance.
1
1247
by: hellostephen | last post by:
Hi. I'm looking for a way reject an xml document with an attribute that has whitespace only. This is not so hard when the attribute is an empty string but the whitespaces has me stumped. BTW...
5
3197
by: Thor W Hammer | last post by:
Smart algorithm for normalizing strings anyone? Example: If the original string looks like this: "the brown fox " I want it end up like this: "the brown fox"
6
2657
by: eight02645999 | last post by:
hi wish to ask a qns on strip i wish to strip all spaces in front of a line (in text file) f = open("textfile","rU") while (1): line = f.readline().strip() if line == '': break print line
9
2401
by: sherifffruitfly | last post by:
Hi, I've a got a little (exercise) program that reads data from a file and puts it into struct members. I run into trouble when one of the data pieces is comprised of several words (eg "john...
6
2300
by: AndyL | last post by:
Hi, I have a lot of sources with mixed indentation typically 2 or 4 or 8 spaces. Is there any way to automatically convert them in let's say 4 spaces? Thx, A.
4
2703
by: kollatjorva | last post by:
Hi all I'm trying to get a value from an xml node 'Publisher' use the value as a name of an .css class. This works fine until I get a value from the Publisher node with white space in it. I've...
10
2295
by: olivier.scalbert | last post by:
Hello, the following constraints: source: <Source> <Item>AAA</Item> <Item>BBBBBBBBBBB</Item> <Item>CCCCCCCCC</Item> <Item>DDDDDDDDDDDDDDD</Item>
1
5454
by: asharda | last post by:
Hi, I have to write a program which will calculate the PEAK and Normalize the audio files. I already have a class which returns an array of in for a given wave file. Does anyone know how I...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7460
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.