beginner's question

H

Hadi

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
 
P

Peter Hansen

Hadi said:
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
 
S

Sean Ross

Hadi said:
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
 
S

Sean Ross

Sean Ross said:
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
 
S

Sean Ross

[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.
 
H

Hadi

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
 
H

Hadi

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

regards,
hadi


Sean Ross said:
[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.
 
H

Hadi

Sean,

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

regards,
hadi
 
H

Hadi

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
 
H

Hadi

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()
 
S

Sean Ross

Hadi said:
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
 
S

Sean Ross

Hadi said:
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
 
D

Daniel 'Dang' Griffith

Sean Ross said:
[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
 
A

Alan Gauld

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
 

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

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top