file input and output

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 5298 Views - Last Post: 05 July 2012 - 06:33 AM Rate Topic: -----

#1 Computer-Wiz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-January 11

file input and output

Posted 08 June 2012 - 08:06 PM

okay so I am reviewing for my year end summative and i came across this problem which I know I have solved before but i cant find the document I have gotten most of this write but its just the last part that messing up.

SO i have to read in this list of fruits text doc and then I have to write it out to another text doc but this time all the fruits need to be in alphabetical order. I have gotten all this done. I even print the list and its all in order. The problem is that when I tell it to write it to the new text doc it just prints the last word. I dont know if its the command that i have wrong or a logical error. Someone please help!!!!!

This is the code everything is straightforward i even tried the indenting but i just cant get it to put the right information in the text doc although i print the list right before and its perfect.

fileIn = open ("listOfFruits.txt", "r")
fileOut = open ("newListOfFruits.txt", "w")

line = []

for currentLine in fileIn:
    currentLine.split(" ")
    line.append(currentLine)

    

    for word in line:
        line.sort()

        fileOut.write(word)
##print line

fileIn.close()
fileOut.close()


Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: file input and output

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: file input and output

Posted 08 June 2012 - 08:30 PM

This algorithm doesn't really work... let's examine it 1 line at a time.

fileIn = open ("listOfFruits.txt", "r")
fileOut = open ("newListOfFruits.txt", "w")

line = []


so far, so good.


for currentLine in fileIn:
    currentLine.split(" ")
    line.append(currentLine)


What? for each line in the file, split that line along the spaces, and then add the original (un-splitted) line to our new list? Why would you do that?


    for word in line:
        line.sort()

        fileOut.write(word)
##print line


A little worse. for each word in the list that we're building, we sort the entire list (If our list has 50 words in it, we sort it 50 times) and then write every word from the list that we've been building to the output file. Thus, you'll be writing the entire list each time you add a new word to it!


fileIn.close()
fileOut.close()


This is good cleanup stuff.


What you end up with is if you have a file containing
a
b
c
d
e
f
g



On the first iteration, you'll write
a

then
a
b

then
a
b
c


which means you'll end up with:
a
a
b
a
b
c
a
b
c
d
a
b
c
d
e
a
b
c
d
e
f
a
b
c
d
e
f
g


This post has been edited by atraub: 08 June 2012 - 08:43 PM

Was This Post Helpful? 3
  • +
  • -

#3 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: file input and output

Posted 08 June 2012 - 11:28 PM

A programmer who knows his file io in python can do this in 7 lines... but I can do it in 5 ;)

This post has been edited by atraub: 08 June 2012 - 11:41 PM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,661
  • Joined: 23-August 08

Re: file input and output

Posted 09 June 2012 - 05:02 AM

LOL...it's Python's "Name that Tune"... Jim, I can write that code in 5 lines.
atraub, WRITE THAT CODE!
Was This Post Helpful? 0
  • +
  • -

#5 Computer-Wiz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-January 11

Re: file input and output

Posted 09 June 2012 - 08:14 AM

i am still confused anyone with a solution or hint i understand what i did wrong but i think its just that line where i want it to write it on the next doc because write before i print it and its printing perfect.

thanks i would really appreciate any help
Was This Post Helpful? 0
  • +
  • -

#6 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: file input and output

Posted 09 June 2012 - 08:23 AM

I'll show you my solution, soon. but I want you to take one more try at it. Here's the algorithm written in pseudo code:

open the input file for reading

read each line of the input file into a list referred to as outputList

close the input file

sort outputList

open the output file

write outputList to the output file

close the output file



Notice that my pseudo code can be clearly written in exactly 7 lines. hint hint...

Sidenote:
You'll notice I changed the file opening and closing a little. It's good practice to only keep a file open as long as absolutely necessary. I open a file only when I need it and close it immediately after I'm done with it.

This post has been edited by atraub: 09 June 2012 - 08:34 AM

Was This Post Helpful? 0
  • +
  • -

#7 Computer-Wiz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-January 11

Re: file input and output

Posted 09 June 2012 - 08:55 AM

im sorry i am just soo confused. havin a bad day. What i have programmed makes sense to me because when i print it i get the right answer and i can remeber doing the same code last time i did this question. i dont know maybe i am just forgetting something really big. however, i know its defneintly with the final line where i tell it to write because everything else seems to make sense.

line = []

fileIn = open ("listOfFruits.txt", "r")
fileOut = open ("newListOfFruits.txt", "w")



for currentLine in fileIn:

    line.append(currentLine)
    

    

    for items in line:
        line.sort()
print line 

fileOut.write(items.strip() + "\n")



fileIn.close()
fileOut.close()

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#8 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: file input and output

Posted 09 June 2012 - 12:38 PM

This is better. Walk me through your code. Explain in plain english what you're doing 1 or two lines at a time. Specifically, help me understand lines 15 - 20.

The solution to your problem is NOT playing with indentation until it works. That is not how you write software. The solution is understanding the algorithm, then designing a program to that models the algorithm. I fear you've already "married" this code and that's why you're reluctant to just rewrite this properly.

This post has been edited by atraub: 09 June 2012 - 02:13 PM

Was This Post Helpful? 0
  • +
  • -

#9 Computer-Wiz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-January 11

Re: file input and output

Posted 09 June 2012 - 02:06 PM

View Postatraub, on 09 June 2012 - 07:38 PM, said:

This is better. Walk me through your code. Explain in plain english what you're doing 1 or two lines at a time. Specifically, help me understand lines 15 - 20.


1)okay so i created an empty list because i know i will have to append items from the file to this list. Then i open up my fileIn and fileOut text files.

2)then for every line in the file i am appending it to the empty list.

3) then for every item in the list i am sorting it alphabetically

4) then i am writing it out to the new text file (but the file is still not printing the right results

5) closing the input and output files
Was This Post Helpful? 0
  • +
  • -

#10 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: file input and output

Posted 09 June 2012 - 02:20 PM

Quote

3) then for every item in the list i am sorting it alphabetically

Why?




Quote

4) then i am writing it out to the new text file

Writing what?

This post has been edited by atraub: 09 June 2012 - 02:23 PM

Was This Post Helpful? 0
  • +
  • -

#11 Computer-Wiz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-January 11

Re: file input and output

Posted 09 June 2012 - 02:27 PM

View Postatraub, on 09 June 2012 - 09:20 PM, said:

Quote

3) then for every item in the list i am sorting it alphabetically

Why?

because the fileIn has the fruit names randomly mixed up so i need to put it into alphabetical order

Quote

Quote

4) then i am writing it out to the new text file

Writing what?

because i need to write the sorted list of fruits to the new text doc

This post has been edited by atraub: 10 June 2012 - 02:04 PM
Reason for edit:: fixed your quote tags

Was This Post Helpful? 0
  • +
  • -

#12 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: file input and output

Posted 09 June 2012 - 06:13 PM

Quote

Quote

Quote

3) then for every item in the list i am sorting it alphabetically

Why?

because the fileIn has the fruit names randomly mixed up so i need to put it into alphabetical order

But why are you calling sort for every item in the list? Why would you need to sort a list more than once?


Quote

Quote

Quote

4) then i am writing it out to the new text file

Writing what?

because i need to write the sorted list of fruits to the new text doc

Your response didn't answer my second question. What are you writing out to the new text file? Pro-tip: that's a hint.

This post has been edited by atraub: 09 June 2012 - 09:06 PM

Was This Post Helpful? 0
  • +
  • -

#13 Computer-Wiz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-January 11

Re: file input and output

Posted 09 June 2012 - 09:10 PM

i understand what you mean can you give me a tip on how i can fix that. I am just confused on the format i have to write my code in. Also i am writing all the fruits names in alphabetical order in the new text doc. I dont understand the algorithms. It would be really helpful if you can provide a sample or answer for this question.

In the fileIn i read the words which were like:

banana
orange
apple

now in the ext file i want them to be sorted an written like this:

apple
banana
orange

in other words all the items in my line list to be written in the new txt doc but in order.
Was This Post Helpful? 0
  • +
  • -

#14 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: file input and output

Posted 09 June 2012 - 10:24 PM

Quote

It would be really helpful if you can provide a sample or answer for this question.

I don't do other people's homework, but I will help you find the right answer.



Quote

But why are you calling sort for every item in the list? Why would you need to sort a list more than once?

You want the list to be sorted, but why would you need to sort the list more than once?



You have said

Quote

for every item in the list i am sorting it [the list] alphabetically
To which I responded: Why would you need to sort a list more than once? Think about it, if you have a list of items, and you sort it, it's sorted! If there are 50 items in your list, you sort the list once, and only once, and the list is now sorted. Why would you need to sort it 50 times?



Quote

Quote

Quote

Quote

Quote

4) then i am writing it out to the new text file

Writing what?

because i need to write the sorted list of fruits to the new text doc

Your response didn't answer my second question. What are you writing out to the new text file? Pro-tip: that's a hint.

i understand what you mean can you give me a tip on how i can fix that. I am just confused on the format i have to write my code in.

You have avoided answering my question for the second time. I will ask this once and only once more, but I'll make it easier and make it an either/or question. Are you trying to write a list of words to the file or are you trying to write the words to the file one at a time? Either way is doable. I will not respond to this thread again unless you make an attempt to answer this question.

This post has been edited by atraub: 09 June 2012 - 10:29 PM

Was This Post Helpful? 0
  • +
  • -

#15 Computer-Wiz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-January 11

Re: file input and output

Posted 10 June 2012 - 07:26 AM

I am trying to write the words one at a time. so all the items in my list are written separately. So if my list is [orange, mango, apple] i want the file to be written like:

apple
mango
orange

*is this what you are asking me?????

This post has been edited by GunnerInc: 11 June 2012 - 04:08 PM
Reason for edit:: Removed unnecessary quotes

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2