469,631 Members | 1,776 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

How to remove a newline character?Please Help

2
Hi All,
I have some contents in a file named Msg.txt. I need to append the date right after the end of every line of that file. I tried out this code, but it did not work.

>>> import datetime
>>> f = open('C:\Python25\Msg1.txt', 'r')
>>> y = datetime.datetime.now().strftime("%d%b%y")
>>> for lines in f:
... z = lines, y
... sr = ""
... sr.join(z)
...
'Hi \n25Feb08'
'this \n25Feb08'
'is \n25Feb08'
'python \n25Feb08'
'remove newline25Feb08'
>>>
I need an output like Hi25Feb08
Please help me to remove the newline character.

Thanks in Advance
Feb 25 '08 #1
3 2380
bvdet
2,851 Expert Mod 2GB
Hi All,
I have some contents in a file named Msg.txt. I need to append the date right after the end of every line of that file. I tried out this code, but it did not work.

>>> import datetime
>>> f = open('C:\Python25\Msg1.txt', 'r')
>>> y = datetime.datetime.now().strftime("%d%b%y")
>>> for lines in f:
... z = lines, y
... sr = ""
... sr.join(z)
...
'Hi \n25Feb08'
'this \n25Feb08'
'is \n25Feb08'
'python \n25Feb08'
'remove newline25Feb08'
>>>
I need an output like Hi25Feb08
Please help me to remove the newline character.

Thanks in Advance
Expand|Select|Wrap|Line Numbers
  1. for line in f:
  2.     z = line.strip(), y
Feb 25 '08 #2
raubana
56
Don't you need to make a 'while loop'? or am i thinking of something else....
Feb 28 '08 #3
jlm699
314 100+
Don't you need to make a 'while loop'? or am i thinking of something else....
When you use a file handle in a for loop it goes until the EOF
Feb 28 '08 #4

Post your reply

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

Similar topics

reply views Thread by Raymond Lewallen | last post: by
6 posts views Thread by 3strands | last post: by
29 posts views Thread by runningdog | last post: by
5 posts views Thread by Adam Right | last post: by
6 posts views Thread by Daniel Mark | last post: by
5 posts views Thread by LEM | last post: by
11 posts views Thread by Michael | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.