accessing a text file

B

Baba

level: beginner

how can i access the contents of a text file in Python?

i would like to compare a string (word) with the content of a text
file (word_list). i want to see if word is in word_list. let's assume
the TXT file is stored in the same directory as the PY file.

def is_valid_word(word, word_list)


thanks
Baba
 
B

Benjamin Kaplan

level: beginner

how can i access the contents of a text file in Python?

i would like to compare a string (word) with the content of a text
file (word_list). i want to see if word is in word_list. let's assume
the TXT file is stored in the same directory as the PY file.

def is_valid_word(word, word_list)


thanks
Baba

Please do us a favor and at least try to figure things out on your
own, rather than coming here every time you have a question. The very
first result when you try searching "python read text file" is the
section in the Python tutorial that explains how to do this.

http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files
 
S

Seth Rees

level: beginner

how can i access the contents of a text file in Python?

i would like to compare a string (word) with the content of a text
file (word_list). i want to see if word is in word_list. let's assume
the TXT file is stored in the same directory as the PY file.

def is_valid_word(word, word_list)


thanks
Baba

f = open('text.txt')
data = f.read()
# You may want to convert it to a list
data = data.split()
# Returns true if word is in data, false otherwise
word in data
 
A

Alexander Kapps

Baba said:
level: beginner

how can i access the contents of a text file in Python?

i would like to compare a string (word) with the content of a text
file (word_list). i want to see if word is in word_list. let's assume
the TXT file is stored in the same directory as the PY file.

def is_valid_word(word, word_list)


thanks
Baba


Completely untested:

def is_valid_word(word, filename):
for line in open(filename):
if word in line.split():
return True
return False
 
Z

Zhu Sha Zang

Em 05-09-2010 19:06, Alexander Kapps escreveu:
Completely untested:

def is_valid_word(word, filename):
for line in open(filename):
if word in line.split():
return True
return False

Damn, <ironic>easy like C/C++/Java</ironic>

Thanks a lot!


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.16 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkyEIQAACgkQ35zeJy7JhCjgpgCgnnYCDj5Axohbhxi3txL6APEO
ANgAniuvPnkRGpOm1ILysFg/nI43YOxg
=W7aK
-----END PGP SIGNATURE-----
 
B

Baba

Please do us a favor and at least try to figure things out on your
own, rather than coming here every time you have a question. The very
first result when you try searching "python read text file" is the
section in the Python tutorial that explains how to do this.

http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-...


Hi Benjamin

I did find that page prior to posting the question but i still wanted
to have a second opinion to complement that info so as to make things
easier The first line of my post clearly states that i am a beginner.
It's nice to provide links which can help answer the question but
please be so polite and keep personal comments for yourself.


To all other respondants: thank you for your kind instructions and
directions.


Thanks
Baba
 
B

Baba

   f = open('text.txt')
   data = f.read()
   # You may want to convert it to a list
   data = data.split()
   # Returns true if word is in data, false otherwise
   word in data

Thanks Seth!
 
T

Thomas Jollans

Hi Benjamin

I did find that page prior to posting the question but i still wanted
to have a second opinion to complement that info so as to make things
easier The first line of my post clearly states that i am a beginner.
It's nice to provide links which can help answer the question but
please be so polite and keep personal comments for yourself.

That is of course perfectly legitimate. It would however have been polite to
state that in the question. Show us that you're doing your homework, and not
just using the list as a cheap path to having to think less yourself.

Phrasing your post as "I would like to compare a ... I found the open()
function here: [link]. Is this what I should use of is there any other/better
way?" makes a completely different impression.

Also, keeping personal comments to one's self is just not how it works. On a
list like this especially, answers along the lines of "That's the way to do
what you were asking for, but are you sure the question went into the right
direction? Have you thought of [...]?" can often be very helpful.
 
B

Baba

Hi Benjamin
I did find that page prior to posting the question but i still wanted
to have a second opinion to complement that info so as to make things
easier The first line of my post clearly states that i am a beginner.
It's nice to provide links which can help answer the question but
please be so polite and keep personal comments for yourself.

That is of course perfectly legitimate. It would however have been polite to
state that in the question. Show us that you're doing your homework, and not
just using the list as a cheap path to having to think less yourself.

Phrasing your post as "I would like to compare a ... I found the open()
function here: [link]. Is this what I should use of is there any other/better
way?" makes a completely different impression.

Also, keeping personal comments to one's self is just not how it works. On a
list like this especially, answers along the lines of "That's the way to do
what you were asking for, but are you sure the question went into the right
direction? Have you thought of [...]?" can often be very helpful.


To all other respondants: thank you for your kind instructions and
directions.

Thanks Thomas. Look up some of my questions this group and read
through them and come back to tell me if a) i use this forum to learn
without making any efforts myself or b) i use this forum to get
started using the expertise of more knowledgeable programmers while at
the same time particiapting. Anyway having this discussion is beside
the point. Any Expert out there who thinks we beginners are some dumb
idiots who are too stupid to think for themselves and are lucky to
have a bunch of geniuses like you to help, get lost or make yourself a
cup of tea but please give me a break from teaching me lessons...

There's lots of nice people out there who are being awsemome by
dedicating a bit of their time to some of our easy questions and it is
them that i wish to send a BIG thanks! Much appreciated. I have made
some good progress since starting to learn Python and this group has
been great help!

tnx
Raoul
 
G

geremy condra

level: beginner
how can i access the contents of a text file in Python?
i would like to compare a string (word) with the content of a text
file (word_list). i want to see if word is in word_list. let's assume
the TXT file is stored in the same directory as the PY file.
def is_valid_word(word, word_list)


Please do us a favor and at least try to figure things out on your
own, rather than coming here every time you have a question. The very
first result when you try searching "python read text file" is the
section in the Python tutorial that explains how to do this.

Hi Benjamin
I did find that page prior to posting the question but i still wanted
to have a second opinion to complement that info so as to make things
easier The first line of my post clearly states that i am a beginner.
It's nice to provide links which can help answer the question but
please be so polite and keep personal comments for yourself.

That is of course perfectly legitimate. It would however have been polite to
state that in the question. Show us that you're doing your homework, and not
just using the list as a cheap path to having to think less yourself.

Phrasing your post as "I would like to compare a ... I found the open()
function here: [link]. Is this what I should use of is there any other/better
way?" makes a completely different impression.

Also, keeping personal comments to one's self is just not how it works. On a
list like this especially, answers along the lines of "That's the way to do
what you were asking for, but are you sure the question went into the right
direction? Have you thought of [...]?" can often be very helpful.


To all other respondants: thank you for your kind instructions and
directions.

Thanks Thomas. Look up some of my questions  this group and read
through them and come back to tell me if a) i use this forum to learn
without making any efforts myself

Just a quick point- when you ask someone for help, it's considered
impolite to tell them what to do.

I'd also point out that you gave no indication that you'd worked on
this at all before posting it. In that regard, Thomas's concern seems
completely justified to me.
or b) i use this forum to get
started using the expertise of more knowledgeable programmers while at
the same time particiapting.

Again, Thomas's concern seems justified to me. Things would probably
go more smoothly if you gave a better indication of what you had done
so far on the problem in the future.
Anyway having this discussion is beside
the point. Any Expert out there who thinks we beginners are some dumb
idiots who are too stupid to think for themselves and are lucky to
have a bunch of geniuses like you to help, get lost or make yourself a
cup of tea but please give me a break from teaching me lessons...

I don't think all beginners are idiots, or even most of them- but this
isn't the right attitude to be taking. Both Thomas and myself thought
that this was inappropriate enough to mention it, and if two people
spoke up you can bet a lot more were thinking it quietly. My
suggestion would be to moderate your approach and demonstrate what
you've done so far (if only to increase the signal-to-noise ratio as
your problems become more challenging) when posting. I'd also refrain
from telling people to get lost; it doesn't make people happy to help
you, you know?

Geremy Condra
 
B

Baba

beginner
how can i access the contents of a text file in Python?
i would like to compare a string (word) with the content of a text
file (word_list). i want to see if word is in word_list. let's assume
the TXT file is stored in the same directory as the PY file.
def is_valid_word(word, word_list)
thanks
Baba
--
http://mail.python.org/mailman/listinfo/python-list
Please do us a favor and at least try to figure things out on your
own, rather than coming here every time you have a question. The very
first result when you try searching "python read text file" is the
section in the Python tutorial that explains how to do this.
http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-...
Hi Benjamin
I did find that page prior to posting the question but i still wanted
to have a second opinion to complement that info so as to make things
easier The first line of my post clearly states that i am a beginner..
It's nice to provide links which can help answer the question but
please be so polite and keep personal comments for yourself.
That is of course perfectly legitimate. It would however have been polite to
state that in the question. Show us that you're doing your homework, and not
just using the list as a cheap path to having to think less yourself.
Phrasing your post as "I would like to compare a ... I found the open()
function here: [link]. Is this what I should use of is there any other/better
way?" makes a completely different impression.
Also, keeping personal comments to one's self is just not how it works.. On a
list like this especially, answers along the lines of "That's the way to do
what you were asking for, but are you sure the question went into the right
direction? Have you thought of [...]?" can often be very helpful.
To all other respondants: thank you for your kind instructions and
directions.
Thanks Thomas. Look up some of my questions  this group and read
through them and come back to tell me if a) i use this forum to learn
without making any efforts myself

Just a quick point- when you ask someone for help, it's considered
impolite to tell them what to do.

I'd also point out that you gave no indication that you'd worked on
this at all before posting it. In that regard, Thomas's concern seems
completely justified to me.
or b) i use this forum to get
started using the expertise of more knowledgeable programmers while at
the same time particiapting.

Again, Thomas's concern seems justified to me. Things would probably
go more smoothly if you gave a better indication of what you had done
so far on the problem in the future.
Anyway having this discussion is beside
the point. Any Expert out there who thinks we beginners are some dumb
idiots who are too stupid to think for themselves and are lucky to
have a bunch of geniuses like you to help, get lost or make yourself a
cup of tea but please give me a break from teaching me lessons...

I don't think all beginners are idiots, or even most of them- but this
isn't the right attitude to be taking. Both Thomas and myself thought
that this was inappropriate enough to mention it, and if two people
spoke up you can bet a lot more were thinking it quietly. My
suggestion would be to moderate your approach and demonstrate what
you've done so far (if only to increase the signal-to-noise ratio as
your problems become more challenging) when posting. I'd also refrain
from telling people to get lost; it doesn't make people happy to help
you, you know?

Geremy Condra

Thanks Jeremy, i will take your advice on board! Noone likes to be
taught lessons i think so it is only normal that i reacted. If i had
received a friendly response from Benjamin (as opposed to "Please do
us a favor and at least try to figure things out on your own") making
me aware of the etiquette that my post should also show that i have
researched my question somehow and if his tone would have been
mannered then we would not be having this discussion now. Ok now i
need to go back to actual Pythoon learning, i'm getting distracted.

Kind regards,
Baba
 
B

Baba

Sloppy wording, I apologise. This should say “… is not respect for a
person”.


This is the main thrust of the message.

--
 \        “What if the Hokey Pokey IS what it's all about?” —anonymous |
  `\                                                                   |
_o__)                                                                  |
Ben Finney

Yes Master :)
 
B

Baba

Yes Master :)

Sloppy wording, I apologise. This should say: If you find the question
you're reading too easy then just don't answer. Noone is the owner of
a democratic forum where freedom to ask the question one likes is
paramount (as long of course as it is related to the group)...so let
me repeat that, to say "Please do us a favour and at least try to
figure things out on your own" is in my view inappropriate. To me it
sounds "Do me a favur and get lost". Can you not understand that? I
accept that it might not have been meant that way but see,
misunderstandings happen easily so a more subtle approach is a
wothwhile effort. My point: i always encourage people to get down
from their ivory towers and to connect to the people...yes they will
sometimes be lazy but they are genuine and very respectful (unlike
what you think)...

no offence now ok, it's not all that serious...open your mind, let
lose all that righteousness and let's enjoy life :)


Baba
 
B

Bruno Desthuilliers

Baba a écrit :
(snip)
If i had
received a friendly response from Benjamin (as opposed to "Please do
us a favor and at least try to figure things out on your own")

According to usenet standards and given your initial question, this is a
_very_ friendly answer.
 
G

Grant Edwards

Sloppy wording, I apologise. This should say: If you find the
question you're reading too easy then just don't answer. Noone is the
owner of a democratic forum where freedom to ask the question one
likes is paramount (as long of course as it is related to the
group)...so let me repeat that, to say "Please do us a favour and at
least try to figure things out on your own" is in my view
inappropriate.

You need to read this:

http://www.catb.org/esr/faqs/smart-questions.html
To me it sounds "Do me a favur and get lost".

No, it means "Do yourself a favor, learn how to do things yourself."

Remember: you're then one asking people to give you something for
free. It's not up to them to conform to your expectations, rather you
need to conform to theirs. Otherwise, they'll just ignore you.
 
B

Baba

You need to read this:

 http://www.catb.org/esr/faqs/smart-questions.html


No, it means "Do yourself a favor, learn how to do things yourself."

Remember: you're then one asking people to give you something for
free. It's not up to them to conform to your expectations, rather you
need to conform to theirs.  Otherwise, they'll just ignore you.

"Please do us a favour" sounds condescending to me at least but maybe
we Europeans are a bit touchy...

However the following Wiki excerpt seems to go in my direction:

"When someone makes a mistake -- whether it's a spelling error or a
spelling flame, a stupid question or an unnecessarily long answer --
be kind about it. If it's a minor error, you may not need to say
anything. Even if you feel strongly about it, think twice before
reacting. Having good manners yourself doesn't give you license to
correct everyone else. If you do decide to inform someone of a
mistake, point it out politely, and preferably by private email rather
than in public. Give people the benefit of the doubt; assume they just
don't know any better. And never be arrogant or self-righteous about
it."

http://en.wikipedia.org/wiki/Netiquette

Baba
 
D

Dennis Lee Bieber

No, it doesn't. It advises that people show kindness; as I've been
arguing, that's exactly what you were shown. You haven't shown how the
information being imparted could have been fully imparted in a way
that's kinder, nor that it would be reasonable to do so.
Be glad I didn't respond to the OP... I don't know what version of
Python they are using but with ActiveState 2.5.x under WinXP on my
systems, my response would have been on the order of...

"
Did you try the help system? Takes less than 10 minutes with
<aforesaid install> to pop up the Python help file, pick the search tab,
and enter
read a file
in the search box, whereupon the first entry is chapter x.y.z of the
included text /Dive Into Python/.
"
 
B

Baba

No, it doesn't. It advises that people show kindness; as I've been
arguing, that's exactly what you were shown. You haven't shown how the
information being imparted could have been fully imparted in a way
that's kinder, nor that it would be reasonable to do so.

To put it another way: if you feel offended by an utterance, then
insufficient kindness on the part of the speaker is not the only
explanation.

--
 \      “Software patents provide one more means of controlling access |
  `\      to information. They are the tool of choice for the internet |
_o__)                                     highwayman.” —Anthony Taylor |
Ben Finney

Hi Ben,

Thanks for your feedback. My question is: Who owns this forum? If we
all do then we are allowed to post questions that are simple and that
could otherwise be answered by doing research. It is just unfriendly
to tell someone to go and look it up by themselves. Who is licensed to
judge what can and cannot be posted as a question? A teacher of mine
used to say: "There are no stupid questions, there are only stupid
answers." It is arrogant to tell someone in a Forum to "look it up
yourself ,this question is too basic". Can you not understand that
accessing a file can seem daunting at first? Believe me, documentation
can be offputting too when you only start with programming. Per se the
beauty of forums like these is that there are human beings willing to
make such tasks as finding out how to access a text file less 'scary'.
Whoever thinks he or she has a license to tell someone to look up the
answer by themselves should think again. I reckon the only license we
have is not to answer at all. I acknowledge that Benjamin pointed me
to the right place to find an answer but somehow the "Please do us a
favour" sounded bit arrogant, as if he owned the place (same goes for
all those who sided against me here: do you own this forum?)...i'd be
glad if there was a python for beginners forum but in the meantime i
have to say i find it awesome to have this forum to get help. It makes
a BIG difference. So sorry if i upset anyone, we all have our opinions
and get carried away and can be stubborn, i DO respect everyone in
this forum and i think it goes bothways. I've learned a few things in
this post...

So now, to use Benjamin words: Please do me a favour and let's move
on :)

Baba

Baba
 
P

Paul Rubin

Baba said:
It is just unfriendly
to tell someone to go and look it up by themselves.

Someone seeing too many unthoughtful questions from you might tell you
to look it up yourself, in the hopes of getting you to change your
questioning style, so that your future questions will be more thoughtful
and worth answering. But according to you that would be unfriendly.

Another thing they could do is say nothing, but quietly configure their
news-reading software to ignore your questions completely. Then none of
your future questions would have any hope of being answered. Would that
be less unfriendly, or more unfriendly?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top