you're telling me what you want to happen, not how you're tying to accomplish it. Is your goal to take the list of words and write it to the file in 1 line of code, or to use a loop to iterate over the list and write each word 1 at a time.
21 Replies - 2145 Views - Last Post: 05 July 2012 - 06:33 AM
#17
Re: file input and output
Posted 11 June 2012 - 02:48 PM
sorry for replying so late.
But i worked on this a little and i came up with another way but this time i am just having problem with writing the items in the list one by one to the next document.
I understood the part about sorting it once and removing all them from being a list into one list. But now i am still stuck on printing the output to the file
But i worked on this a little and i came up with another way but this time i am just having problem with writing the items in the list one by one to the next document.
line = []
fileIn = open ("listOfFruits.txt", "r")
fileOut = open ("newListOfFruits.txt", "w")
for currentLine in fileIn:
line.append(currentLine)
for items in line:
line.sort()
fileOut.write(items.strip() + "\n")
print line
fileIn.close()
fileOut.close()
I understood the part about sorting it once and removing all them from being a list into one list. But now i am still stuck on printing the output to the file
This post has been edited by GunnerInc: 11 June 2012 - 04:07 PM
Reason for edit:: Removed unnecessary quotes
#18
Re: file input and output
Posted 11 June 2012 - 03:57 PM
sorry this is the actual code that i just recently did:
fileIn = open ("listOfFruits.txt", "r")
fileOut = open ("newListOfFruits.txt", "w")
line =[]
for currentLine in fileIn:
line.append(currentLine.strip())
line.sort()
for items in line:
fileOut.write(items)
fileIn.close()
fileOut.close()
This post has been edited by GunnerInc: 11 June 2012 - 04:06 PM
Reason for edit:: Removed unnecessary quotes
#19
Re: file input and output
Posted 11 June 2012 - 07:52 PM
Good work!!! I was starting to doubt you, but this code shows significant improvement. Because of your effort, I will show you the missing piece. Believe it or not, the piece you're missing was in one of your earlier revisions! Here's your code, working properly:
Now, here's a minor improvement:
Notice how I moved the opening and closing around, this ensures that the files are open for a minimal amount of time. In file IO, we always want to have files opened for as little as possible. Now, let's assume that every line ended in a new line character (including the last line) then you could do something like this.
and here's my 5 line version
Of course, both of these versions assume that ALL lines end with a new line character, if the last one doesn't, I'd need to add a little something extra in to account for that. What would I add, you ask? Here's the FINAL revision:
I should point out that I would never actually use code this ugly, there's nothing impressive about code that's tough to read. However, I did want to do this in 5 lines, so I figured, what the hell? I dynamically generate a list, and as I'm building that list, I add a new line to any lines that don't end in a new line character. This combines 3 techniques to achieve my goal:
list comprehensions - to build the list
the sorted operator - which returns a sorted list
python's ternary syntax
Hope this helps, and I hope you found it interesting.
fileIn = open ("listOfFruits.txt", "r")
fileOut = open ("newListOfFruits.txt", "w")
line =[]
for currentLine in fileIn:
line.append(currentLine.strip())
line.sort()
for items in line:
fileOut.write(items+"\n") #You forgot to re-add the new line character
fileIn.close()
fileOut.close()
Now, here's a minor improvement:
fileIn = open ("listOfFruits.txt", "r")
line =[]
for currentLine in fileIn:
line.append(currentLine.strip())
fileIn.close()
line.sort()
fileOut = open ("newListOfFruits.txt", "w")
for items in line:
fileOut.write(items+"\n")
fileOut.close()
Notice how I moved the opening and closing around, this ensures that the files are open for a minimal amount of time. In file IO, we always want to have files opened for as little as possible. Now, let's assume that every line ended in a new line character (including the last line) then you could do something like this.
#7 lines
fileIn = open ("listOfFruits.txt", "r")
line = fileIn.readlines()
fileIn.close()
line.sort()
fileOut = open ("newListOfFruits.txt", "w")
fileOut.writelines(line)
fileOut.close()
and here's my 5 line version
fileIn = open ("listOfFruits.txt", "r")
fileOut = open ("newListOfFruits.txt", "w")
fileOut.writelines(sorted(fileIn.readlines()))
fileIn.close()
fileOut.close()
Of course, both of these versions assume that ALL lines end with a new line character, if the last one doesn't, I'd need to add a little something extra in to account for that. What would I add, you ask? Here's the FINAL revision:
fileIn = open ("listOfFruits.txt", "r")
fileOut = open ("newListOfFruits.txt", "w")
fileOut.writelines(sorted([line+"\n" if line[-1] != "\n" else line for line in fileIn.readlines()]))
fileIn.close()
fileOut.close()
I should point out that I would never actually use code this ugly, there's nothing impressive about code that's tough to read. However, I did want to do this in 5 lines, so I figured, what the hell? I dynamically generate a list, and as I'm building that list, I add a new line to any lines that don't end in a new line character. This combines 3 techniques to achieve my goal:
list comprehensions - to build the list
the sorted operator - which returns a sorted list
python's ternary syntax
Hope this helps, and I hope you found it interesting.
This post has been edited by atraub: 11 June 2012 - 08:08 PM
#20
Re: file input and output
Posted 13 June 2012 - 04:33 PM
Thanks for all the help and assistance, i really appreciated it, it helped a lot. Now i am able to understand the algorithms more easily.
#21
Re: file input and output
Posted 13 June 2012 - 06:54 PM
Well, I'm always happy to help. Don't forget to click
if my post was helpful to you
if my post was helpful to you #22
Re: file input and output
Posted 05 July 2012 - 06:33 AM
|
|

New Topic/Question
Reply





MultiQuote




|