468,458 Members | 1,764 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,458 developers. It's quick & easy.

removing line breaks

i was trying to code a morse code decipherer which workd fine except that the .txt file that i was converting had linebreaks built in, i cant get python to ignore the line breaks and i cant find any characters for line breaks to remove them using the string.replace() function

if anybody know the syntax for a line break or the ASCII characters that can be used for line breaks to replace/remove them that would be great

or if anybody know how to read multiple lines as a single uninterrupted string that would be even beetter

caspar

PS i have tried for line in file() and then string = string+line and this still creates line breaks in the string even though it is a single string
Jul 12 '06 #1
2 25485
solved it, just in case anyone else was wondering if reading from a text file for example use the following

string=""
for line in file(filename):
string+=line.replace("\n","")
print string

at this point string is a single uninterrupted string you can work with

caspar
Jul 18 '06 #2
dvs
12
Expand|Select|Wrap|Line Numbers
  1. print ''.join(open('the_file.txt').read().splitlines())
Jul 20 '06 #3

Post your reply

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

Similar topics

5 posts views Thread by Mechphisto | last post: by
6 posts views Thread by Aaron Collins | last post: by
1 post views Thread by ChrisC | last post: by
2 posts views Thread by Mike | last post: by
13 posts views Thread by scorpion53061 | last post: by
18 posts views Thread by mouseit101 | last post: by
5 posts views Thread by joelbyrd | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.