Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 6th, 2004, 03:53 PM
caroundw5h caroundw5h is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Canada
Posts: 181 caroundw5h User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 55 m 53 sec
Reputation Power: 0
What does this do?

What does the following python code do?

PHP Code:
 import time
file 
"whatever.txt"
count 0

open(file,"r+")
for 
line in a:
    
count+=1
    line
.strip()
    
line.lower()
    print 
"editing line "count

print "last edited :",  time.asctime()
a.close() 
__________________
"In theory, there is no difference between theory and practice.
But, in practice, there is."


Reply With Quote
  #2  
Old August 6th, 2004, 11:13 PM
rebbit rebbit is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 84 rebbit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 7 m
Reputation Power: 5
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.

Reply With Quote
  #3  
Old August 7th, 2004, 03:28 AM
caroundw5h caroundw5h is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Canada
Posts: 181 caroundw5h User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 55 m 53 sec
Reputation Power: 0
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?

Reply With Quote
  #4  
Old August 7th, 2004, 05:54 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.
__________________
programming language development: www.netytan.com Hula


Last edited by netytan : August 7th, 2004 at 05:58 AM.

Reply With Quote
  #5  
Old August 7th, 2004, 12:14 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,226 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 23 h 38 m 24 sec
Reputation Power: 263
Quote:
Originally Posted by netytan
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.



I would suggest closing the first file before opening the second.

Dave - The Developers' Coach

Reply With Quote
  #6  
Old August 7th, 2004, 03:39 PM
caroundw5h caroundw5h is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Canada
Posts: 181 caroundw5h User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 55 m 53 sec
Reputation Power: 0
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?

Reply With Quote
  #7  
Old August 7th, 2004, 08:18 PM
rebbit rebbit is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 84 rebbit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 7 m
Reputation Power: 5
Quote:
Originally Posted by DevCoach
I would suggest closing the first file before opening the second.

Dave - The Developers' Coach


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:
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?


you need to open the file for reading and writing separately. modifying the file while you're reading it could cause problems.

Reply With Quote
  #8  
Old August 8th, 2004, 01:13 PM
caroundw5h caroundw5h is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Canada
Posts: 181 caroundw5h User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 55 m 53 sec
Reputation Power: 0
Quote:
Originally Posted by rebbit
you need to open the file for reading and writing separately. modifying the file while you're reading it could cause problems.


guess that explains why it erased my data. Thanks for all of your replies.

Reply With Quote
  #9  
Old August 9th, 2004, 04:21 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
Quote:
Originally Posted by rebbit
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.


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.
__________________
Debian - because life's too short for worrying.
Best. (Python.) IRC bot. ever.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > What does this do?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT