Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > Python

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-12-2010, 03:24 PM   PM User | #1
whatg
New Coder

 
Join Date: Jan 2010
Posts: 45
Thanks: 6
Thanked 0 Times in 0 Posts
whatg is an unknown quantity at this point
Exclamation Read File

Am new to python so I decided to take some notes in .txt files and thought it would be a good idea if I had program that could read them! So I set out to do this. All is well except whenever there is a new line in the text file python prints "\n" instead of taking a new line.
This is my code:


name=raw_input("Which file would you like to read?")

second=".txt"

name = "%s%s" % (name,second)


file = open(name)

for line in file:
print line,


Text File:
Python is great
I love python
but why is it doing this?

Result:
Python is great \n I love python \n but why is it doing this?
What am I doing wrong!? Thanks
whatg is offline   Reply With Quote
Old 07-12-2010, 04:49 PM   PM User | #2
whatg
New Coder

 
Join Date: Jan 2010
Posts: 45
Thanks: 6
Thanked 0 Times in 0 Posts
whatg is an unknown quantity at this point
Solved!

Solved!!
whatg is offline   Reply With Quote
Old 07-13-2010, 11:00 AM   PM User | #3
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
Originally Posted by whatg View Post
Solved!!
Share your solution please...
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 07-13-2010, 09:26 PM   PM User | #4
whatg
New Coder

 
Join Date: Jan 2010
Posts: 45
Thanks: 6
Thanked 0 Times in 0 Posts
whatg is an unknown quantity at this point
Sorry

Lol sorry, that would be a good idea!

Yeah when I was experimenting I put the new line characters into the text file so thats why they were showing up. Now I am having a problem taking a new line when there is one in the file e.g.

File:
Hi
Bye

Program Prints:
Hi Bye

Have been following the python guide but I seem to be doing something wrong, I can only seem to able to read the whole file at once and not just one line?!
whatg is offline   Reply With Quote
Old 07-14-2010, 08:51 PM   PM User | #5
Samhain13
Regular Coder

 
Samhain13's Avatar
 
Join Date: Aug 2008
Location: Pilipinas
Posts: 165
Thanks: 4
Thanked 18 Times in 18 Posts
Samhain13 is on a distinguished road
^ Well, nobody can tell you what's wrong with what you're doing if nobody can see it. But you should probably read up on read() and readlines(); they're very useful when you're working with text files.
__________________
I am a Man of Truth. I am a Free Human Person. I am a Peacemaker.
** Independent Multimedia Artist in Pasig **
Samhain13 is offline   Reply With Quote
Old 07-15-2010, 10:02 AM   PM User | #6
whatg
New Coder

 
Join Date: Jan 2010
Posts: 45
Thanks: 6
Thanked 0 Times in 0 Posts
whatg is an unknown quantity at this point
Mac

Done some more research and found that it doesn't work on mac os x because of some GNU licence agreement. I tried my program on linux and it worked fine. Am now looking at how to get it to work on the mac..

Does anyone know how to do that?

I had to chose the method that didn't work lol :-P
whatg is offline   Reply With Quote
Old 07-16-2010, 03:31 PM   PM User | #7
Samhain13
Regular Coder

 
Samhain13's Avatar
 
Join Date: Aug 2008
Location: Pilipinas
Posts: 165
Thanks: 4
Thanked 18 Times in 18 Posts
Samhain13 is on a distinguished road
Whatever. If you don't show people here how you're trying to do things, nobody can help you.
__________________
I am a Man of Truth. I am a Free Human Person. I am a Peacemaker.
** Independent Multimedia Artist in Pasig **
Samhain13 is offline   Reply With Quote
Old 07-16-2010, 05:39 PM   PM User | #8
whatg
New Coder

 
Join Date: Jan 2010
Posts: 45
Thanks: 6
Thanked 0 Times in 0 Posts
whatg is an unknown quantity at this point
what do you mean?

Quote:
Originally Posted by Samhain13 View Post
Whatever. If you don't show people here how you're trying to do things, nobody can help you.
I am showing you what I'm doing, included the program source, its output and the desired result. I don't know how more clear I can be.

file.readline() does not work on mac for some strange reason!

Does anyone know how to solve this?
whatg is offline   Reply With Quote
Old 07-17-2010, 01:28 PM   PM User | #9
Samhain13
Regular Coder

 
Samhain13's Avatar
 
Join Date: Aug 2008
Location: Pilipinas
Posts: 165
Thanks: 4
Thanked 18 Times in 18 Posts
Samhain13 is on a distinguished road
Quote:
file.readline() does not work on mac for some strange reason!
Does anyone know how to solve this?
Uhm. Put an s in file.readline? Like: file.readlines(). You should also catch the result in a variable because readlines() is going to return a list. But again, what are you trying to do and how are you trying to do it?
__________________
I am a Man of Truth. I am a Free Human Person. I am a Peacemaker.
** Independent Multimedia Artist in Pasig **

Last edited by Samhain13; 07-17-2010 at 01:31 PM..
Samhain13 is offline   Reply With Quote
Old 07-17-2010, 09:58 PM   PM User | #10
whatg
New Coder

 
Join Date: Jan 2010
Posts: 45
Thanks: 6
Thanked 0 Times in 0 Posts
whatg is an unknown quantity at this point
readlines

I did try the readlines and it worked. I just didn't know how I would go about printing each line of text on a new line.

I simply want to read a text file and print it out in IDLE and so that when there is a new line of text this is represented in IDLE.
whatg is offline   Reply With Quote
Old 07-23-2010, 06:13 PM   PM User | #11
whatg
New Coder

 
Join Date: Jan 2010
Posts: 45
Thanks: 6
Thanked 0 Times in 0 Posts
whatg is an unknown quantity at this point
Soloution

When creating the file object use f = (filename, 'rU')

apparently the 'U' tells python to ignore the Unix line endings.

so the code would look something like this.

filename=raw_input("Give me a filename: ")

f = (filename, 'rU')

for line in f:
print line,
whatg is offline   Reply With Quote
Old 07-23-2010, 06:13 PM   PM User | #12
whatg
New Coder

 
Join Date: Jan 2010
Posts: 45
Thanks: 6
Thanked 0 Times in 0 Posts
whatg is an unknown quantity at this point
If only

Quote:
Originally Posted by whatg View Post
When creating the file object use f = (filename, 'rU')

apparently the 'U' tells python to ignore the Unix line endings.

so the code would look something like this.

filename=raw_input("Give me a filename: ")

f = (filename, 'rU')

for line in f:
print line,

If only I could thank myself.. lol :P
whatg is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:40 PM.


Advertisement
Log in to turn off these ads.