472,328 Members | 1,102 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

beginner's question

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
13 1468
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

"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

"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

"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
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
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
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
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
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

"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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Svens | last post by:
Hey everyone! I'm a math student working on a short script involving logs. I have a function on my scientific calculator, and was wondering if...
3
by: jvax | last post by:
Hi all, I hope I'm posting in the right NG... I have a data text file I want to read from a c++ program. the data file goes like this: 90 #...
1
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that...
14
by: z_learning_tester | last post by:
But I can't seem to find the answer. The question is how do you reverse the words in a string? Or how do you reverse the numbers listed in a...
12
by: Blaze | last post by:
I am doing the first walk through on the Visual Studio .Net walkthrough book to learn a little about programming. I am having issues with the first...
5
by: optimistx | last post by:
As a beginner in javascript I had a question. I was reading FAQ and posts here. I became very unhappy: Obviously this group is mainly for wise,...
10
by: Roman Zeilinger | last post by:
Hi I have a beginner question concerning fscanf. First I had a text file which just contained some hex numbers: 0C100012 0C100012 ....
4
by: a | last post by:
Dear all vb.net developer I want to know the time I need to master vb.net? I'm beginner
3
by: Ben Keshet | last post by:
I have a probably simple beginner's question - I have a script that I am currently able to print its output. instead, i want to write it into a...
2
by: roanhn | last post by:
Hello. I've to to write a master's thesis. Currently I deal with php, mysql, ajax. Fate decreed that I've to choose one of this subjects: 1.gdi+...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.