|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
What does this do?
What does the following python code do?
PHP Code:
__________________
"In theory, there is no difference between theory and practice. But, in practice, there is."
|
|
#2
|
|||
|
|||
|
Code:
import time
file = "whatever.txt"
count = 0 #stores the current line of the text file
a = open(file,"r+") #open file for reading
for line in a: #cycle through lines in the file
count+=1 #updates count to indicate the next line
line.strip() #remove whitespace
#should be 'l = line.strip()' to store the return value
line.lower() #returns the string converted to lowercase.
#should be 'l = line.lower()' to store the return value
print "editing line ", count #prints the current line
print "last edited :", time.asctime() #prints the time
a.close()
the script really has no final result, it doesn't modify the text file in any way or anything like that. I suppose as a side effect the script tells you how many lines are in a text file. |
|
#3
|
|||
|
|||
|
thanks for replying. The problem was I wanted it to clear whitespace and lower() everything, but the file returned nothing modigfied. What was it I was doing wrong?
|
|
#4
|
||||
|
||||
|
The code effectivly does nothing usful - it iterates over each line in the file and prints the number number - since it doesn't write the modifide lines to the file, so when the program exits, no changes are saved.
Code:
#!/usr/bin/env python
path = 'input.txt'
count = 1
lines = []
for line in file(path):
line = line.strip()
line = line.lower()
lines.append(line)
print 'Editing', count
count = count + 1
file(path, 'w').writelines(lines)
Here the lines are appended to the 'lines' list before writing them to the file at the end. Note: this is untested code but should work fine. Hope this helps, Mark. Last edited by netytan : August 7th, 2004 at 05:58 AM. |
|
#5
|
|||
|
|||
|
Quote:
I would suggest closing the first file before opening the second. Dave - The Developers' Coach |
|
#6
|
|||
|
|||
|
why do you have to assigning and such, isn't there a way to just have it go over the file, modify it and save it? why the need for a making a separate list and putting the .strip() and lower() methods results in that list?
![]() |
|
#7
|
||||
|
||||
|
Quote:
The first file object should be destroyed by Python itself, as there are no longer any references to it once the for loop has finished, at least that is my understanding of it. a call to locals()/globals() after a for loop like that indicates there is no file object in either namespace. Quote:
you need to open the file for reading and writing separately. modifying the file while you're reading it could cause problems. |
|
#8
|
|||
|
|||
|
Quote:
guess that explains why it erased my data. Thanks for all of your replies. ![]() |
|
#9
|
|||
|
|||
|
Quote:
You're right, things defined within the for statement are not in the parent scope. Observe. Code:
>>> class A(list): pass
...
>>> for i in A([1, 2, 3, 4]):
... print i
...
1
2
3
4
>>> locals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', 'i': 4, 'A': <class '__main__.A'>, '__doc__': None}
>>>
Note that there's no instance of my A class defined, just the class itself. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > What does this do? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|