By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,171 Members | 776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,171 IT Pros & Developers. It's quick & easy.

beginner's question

P: n/a
Hi,
I have two files and the array that contains 20 words in it.
I'm trying to write python script that suppose achieve the following:
-Open file-1, read the line that contains 20 words separated with commas
-Compare each word with words in the array
-Open file-2
-if the word appears in the array write 'true' in file-2
-if not write 'false'
-repeat this for every line in the file-1

so, two file will be identical in terms of number of lines and number of
words in each line.
However, file-2 will only contain wods 'true' and 'false'.

Can anyone give some example or point me to som useful web sites please.

Thanks,
hadi
Jul 18 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Hadi wrote:
I have two files and the array that contains 20 words in it.
I'm trying to write python script that suppose achieve the following:
-Open file-1, read the line that contains 20 words separated with commas
-Compare each word with words in the array
-Open file-2
-if the word appears in the array write 'true' in file-2
-if not write 'false'
-repeat this for every line in the file-1

so, two file will be identical in terms of number of lines and number of
words in each line.
However, file-2 will only contain wods 'true' and 'false'.

Can anyone give some example or point me to som useful web sites please.


Can you post some sample code that you've written to show that you've
actually tried by yourself first? That will also give us a starting
point in helping you.

Otherwise we're likely to be spoonfeeding you answers to your homework
assignment. Presumably you have already learned some basic programming
in class and are supposed to be trying to apply that learning in this
exercise?

Hints: How do you open a file in Python and read lines from it? (see
the tutorial online at www.python.org) How do you define an "array"
in Python? (see tutorial again, looking at dictionaries) How do you
write a loop in Python? (use the 'for' or 'while' keywords, depending
on what exactly you want to do: see the tutorial for examples)

-Peter
Jul 18 '05 #2

P: n/a

"Hadi" <ha**@nojunk.com.au> wrote in message
news:c8**********@lust.ihug.co.nz...
Hi,
I have two files and the array that contains 20 words in it.
I'm trying to write python script that suppose achieve the following:
-Open file-1, read the line that contains 20 words separated with commas
-Compare each word with words in the array
-Open file-2
-if the word appears in the array write 'true' in file-2
-if not write 'false'
-repeat this for every line in the file-1

so, two file will be identical in terms of number of lines and number of
words in each line.
However, file-2 will only contain wods 'true' and 'false'.

Can anyone give some example or point me to som useful web sites please.

Thanks,
hadi


lexicon = ["our", "list", "of", "words", "to", "be", "compared"]

# open the source and destination files
src = file("file1/path") # open for reading
dst = file("file2/path", 'w') # open for writing

# Hadi: does file1 contain one or more lines? This only works for one line.
# For more lines, you'll need to do further processing ...
#
# Make a list of the words in src. We split the line at each comma, and
# remove any excess whitespace from around the word
words = [w.strip() for w in src.readline().split(',')]

# Now we see if the words are in our list and write the results to dst
for w in words:
dst.write(str(w in lexicon)) # writes 'True, ' or 'False, '

# Python will eventually close the files for you, or you can do it
explicitly
src.close()
dst.close()

Hope that helps,

Sean
Jul 18 '05 #3

P: n/a

"Sean Ross" <sr***@connectmail.carleton.ca> wrote in message
news:Xs********************@news20.bellglobal.com. ..
dst.write(str(w in lexicon)) # writes 'True, ' or 'False, '


The above code will run the True/False output together, e.g.
"TrueTrueFalseTrueFalse".
I doubt that's what you want. Perhaps this, instead:

dst.write("%s "%(w in lexicon))

That would write "True True False True False".

Sean
Jul 18 '05 #4

P: n/a

"Peter Hansen" <pe***@engcorp.com> wrote in message
news:dr********************@powergate.ca...
[snip]
Otherwise we're likely to be spoonfeeding you answers to your homework
assignment.

[snip]

Hm. I've already done the 'spoonfeeding'. Didn't really think about it.
Sorry about that. From now on, I'll have atleast one cup of coffee in the
morning before I start replying to posts.
Jul 18 '05 #5

P: n/a
Peter,

You're right. This is related to my assignment. I've checked the website
you've pointed me but I didn't find it useful. First, I need to know what
python file looks like, how to declare attributes etc.

regards,
hadi
Can you post some sample code that you've written to show that you've
actually tried by yourself first? That will also give us a starting
point in helping you.

Otherwise we're likely to be spoonfeeding you answers to your homework
assignment. Presumably you have already learned some basic programming
in class and are supposed to be trying to apply that learning in this
exercise?

Hints: How do you open a file in Python and read lines from it? (see
the tutorial online at www.python.org) How do you define an "array"
in Python? (see tutorial again, looking at dictionaries) How do you
write a loop in Python? (use the 'for' or 'while' keywords, depending
on what exactly you want to do: see the tutorial for examples)

-Peter

Jul 18 '05 #6

P: n/a
what's wrong with helping to new beginners?
Don't be snob!

regards,
hadi
"Sean Ross" <sr***@connectmail.carleton.ca> wrote in message
news:pI********************@news20.bellglobal.com. ..

"Peter Hansen" <pe***@engcorp.com> wrote in message
news:dr********************@powergate.ca...
[snip]
Otherwise we're likely to be spoonfeeding you answers to your homework
assignment.

[snip]

Hm. I've already done the 'spoonfeeding'. Didn't really think about it.
Sorry about that. From now on, I'll have atleast one cup of coffee in the
morning before I start replying to posts.

Jul 18 '05 #7

P: n/a
Sean,

I am grateful for your help.
Sorry for the previous reply.
God bless you. Thank you.

regards,
hadi
"Sean Ross" <sr***@connectmail.carleton.ca> wrote in message
news:Ny********************@news20.bellglobal.com. ..

"Sean Ross" <sr***@connectmail.carleton.ca> wrote in message
news:Xs********************@news20.bellglobal.com. ..
dst.write(str(w in lexicon)) # writes 'True, ' or 'False, '


The above code will run the True/False output together, e.g.
"TrueTrueFalseTrueFalse".
I doubt that's what you want. Perhaps this, instead:

dst.write("%s "%(w in lexicon))

That would write "True True False True False".

Sean

Jul 18 '05 #8

P: n/a
Sean,
file-1 contains about 500 lines.

Also when I run the below script, I am getting following error in the
command line:

File "<stdin>", line 1
python test.py

SyntaxError: syntax error

Any suggestion about this?

Thanks again.
hadi
lexicon = ["our", "list", "of", "words", "to", "be", "compared"]

# open the source and destination files
src = file("file1/path") # open for reading
dst = file("file2/path", 'w') # open for writing

# Hadi: does file1 contain one or more lines? This only works for one line. # For more lines, you'll need to do further processing ...
#
# Make a list of the words in src. We split the line at each comma, and
# remove any excess whitespace from around the word
words = [w.strip() for w in src.readline().split(',')]

# Now we see if the words are in our list and write the results to dst
for w in words:
dst.write(str(w in lexicon)) # writes 'True, ' or 'False, '

# Python will eventually close the files for you, or you can do it
explicitly
src.close()
dst.close()

Hope that helps,

Sean

Jul 18 '05 #9

P: n/a
Sean,

I appreciate for your help. Your script works fine. I have one last
question.
What do I have to modify on the script, so the script will work for more
than one line?
Because the file-1 has about 500 lines.
This is for my Machine Learning assignment which I use this for the Naive
Bayes classifier for the spam mail classification.
Thank you again and I would appreciate very much one more help from you.

Regards,
Halit

your script:

exicon = ["our", "halit", "of", "words", "to", "be", "compared"]

# open the source and destination files
src = file("file-1.txt") # open for reading
dst = file("file-2.txt", 'w') # open for writing

# Hadi: does file1 contain one or more lines? This only works for one line.
# For more lines, you'll need to do further processing ...
#
# Make a list of the words in src. We split the line at each comma, and
# remove any excess whitespace from around the word
words = [w.strip() for w in src.readline().split(',')]

# Now we see if the words are in our list and write the results to dst
for w in words:
dst.write("%s, "%(w in lexicon))

# Python will eventually close the files for you, or you can do it
explicitly
src.close()
dst.close()
Jul 18 '05 #10

P: n/a

"Hadi" <ha**@nojunk.com.au> wrote in message
news:c8**********@lust.ihug.co.nz...
Sean,
file-1 contains about 500 lines.

Also when I run the below script, I am getting following error in the
command line:

File "<stdin>", line 1
python test.py

SyntaxError: syntax error

Any suggestion about this?

Thanks again.
hadi


[snip]
# open the source and destination files
src = file("file1/path") # open for reading
dst = file("file2/path", 'w') # open for writing

[snip]
Well, I'm not sure.

Here, I've made a test file called "words.txt" in the same directory where
I'm running the program. The file contains:

our, list, of , words, but, some, are, not, in, the, lexicon

all on one line. so for src I do

src = file("words.txt")

And for dst I create an "output.txt" file

dst = file("output.txt", 'w')

The rest of the program is unmodified, except for the change from str(w in
lexicon) to "%s "%(w in lexicon). When I run the program I receive no
errors, and the output.txt file contains:

True True True True False False False False False False False

So, it appears to work fine over the data (and the manner in which it was
stored) for which it was written. Your data (in file-1) is not stored in the
same way so you'll need to augment the code to handle those differences. To
get more detailed help it would be a good idea to post the code you have
written, give a sample of what file-1 contains, and give an example of what
you want file-2 to look like.

Oh, and there's nothing wrong with helping beginners (a lot of us read this
group hoping to do so), it's just that sometimes it is *more* helpful to
show you how to learn how to do things on your own, rather than giving you
the answers directly. So, when I apologized in that post to Peter, most of
that apology would have been directed towards you - I did you a disservice
by not allowing you to learn how to learn. It may not always seem that way,
when you just want the answers, but it's true, nevertheless.

Sean


Jul 18 '05 #11

P: n/a

"Hadi" <ha**@nojunk.com.au> wrote in message
news:c8**********@lust.ihug.co.nz...
Sean,

I appreciate for your help. Your script works fine. I have one last
question.
What do I have to modify on the script, so the script will work for more
than one line?
Because the file-1 has about 500 lines.
This is for my Machine Learning assignment which I use this for the Naive
Bayes classifier for the spam mail classification.
Thank you again and I would appreciate very much one more help from you.

Regards,
Halit

[snip]

Well, you have a file (file-1), which you now know how to open.

src = file("file-1.txt")

There are a few ways to proceed, but I'll choose one and stick with that.
You'll want to go through the file, one line at a time (it's possible to
read in the entire file and process it that way, but we'll skip that for
now). You can go through a file one line at a time using a for loop:

for line in src:
... do stuff with line ...
Now, in the code I posted earlier I used a list comprehension where I read
one line of the file and extracted the words (storing them in a list called
words).

words = [w.strip() for w in src.readline().split(',')]
Using the for loop, that line has already been read so you do not need to
use src.readline() any more, but you still need to do the processing on the
line to extract the words. I think you should be able to figure out how to
do that, so I won't show the code changes here. Once you have the list of
words for that line, you need to see whether they are in your lexicon and
write True or False to your output file (file-2.txt, in this case). The code
I provided earlier gives an example of how to do that. If you need the
output to be in a certain format, other than what the example shows, you
should be able to figure out how to get that done as well.

So, to recap, you'll be using a for loop to go through the input file, one
line at a time. For each line, you will extract the words and store them in
a list. Then, for each word in that list, you will check whether it is in
your lexicon, and write either True or False to your output file. When
you're done, you can close both files explicitly, or let Python take care of
it for you.

That should be enough to get you started. If you have more difficulties,
post your code, and people will help point you in the right direction.

Good luck,
Sean
Jul 18 '05 #12

P: n/a
On Sat, 15 May 2004 00:10:15 +1000, "Hadi" <ha**@nojunk.com.au> wrote:
"Sean Ross" <sr***@connectmail.carleton.ca> wrote in message
news:pI********************@news20.bellglobal.com ...

"Peter Hansen" <pe***@engcorp.com> wrote in message
news:dr********************@powergate.ca...
[snip]
> Otherwise we're likely to be spoonfeeding you answers to your homework
> assignment.

[snip]

Hm. I've already done the 'spoonfeeding'. Didn't really think about it.
Sorry about that. From now on, I'll have atleast one cup of coffee in the
morning before I start replying to posts.


what's wrong with helping to new beginners?
Don't be snob!


He's not being a snob. Please understand than on the internet, many
students will try to get the public to answer their homework for them,
without doing the work on their own. Your question sounds like a
typical beginner's programming assignment. Because you gave no
indication of what you had tried already, it looked like you were
trying to just get a quick answer without doing any learning on your
own. Peter is very helpful when it is clear that someone has tried to
resolve the problem on their own.

Good luck with future posts!
--dang
Jul 18 '05 #13

P: n/a
On Mon, 17 May 2004 12:51:55 GMT, Daniel 'Dang' Griffith
<no*****@noemail4u.com> wrote:
what's wrong with helping to new beginners?
Don't be snob!


He's not being a snob. Please understand than on the internet, many
students will try to get the public to answer their homework for them,
without doing the work on their own.


And FWIW the same policies are applied on the Python tutor
mailing list which is specifically for beginners, but we still
expect to see some evidence of effort from the poster.

OTOH If you are a beginner you may find the tutor list worth
subscribing to, just post the right kind of questions and you'll
get lots of help.

Alan G.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld
Jul 18 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.