Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

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 February 8th, 2004, 10:53 AM
Boceifus's Avatar
Boceifus Boceifus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 103 Boceifus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 1 h 17 m 20 sec
Reputation Power: 10
yet another append question

greets

i found this handy function by Netytan to insert strings into a text file.

Code:
#Netytan Insert Function

def sappend(file, string, line = 0, out = file):
	file = open(file, 'r').readlines()
	file.insert(line, string)
	open(out, 'w').writelines(file)

sappend('file.txt', 'baby\n', 3, 'new.txt')


works great,but i have just a few questions.please forgive me if my wording is incorrect.

1)can i make the string replace the line,not just insert the string into the line?

2)is there a way i can make the line number a result of raw_input?i am trying to do something like: sappend(fn, string, ln, fn2)

Code:
def sappend(file, string, line = 0, out = file):
    
	file = open(file, 'r').readlines()
	file.insert(line, string)
	open(out, 'w').writelines(file)


fn= raw_input('file to edit?')
fn2= raw_input('save name?')
ln= raw_input('line number?')
string= raw_input('string to add?')

sappend (fn, string, ln, fn2)

but i get this error when i use "ln" in the sappend line:
TypeError: an integer is required

but "ln" IS a number...can anyone please help?thx in advance
__________________
It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?

Reply With Quote
  #2  
Old February 8th, 2004, 12:43 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 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 17 m 47 sec
Reputation Power: 68
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
because of the nature of the insert method you can't really make it replace the line but you can simply overwrite the value of that line like so...

Code:
def rappend(path, string, index = 0, output = path):
	lines = file(path, 'r').readlines()
	lines[index] = string
	file(output, 'w').writelines(lines)


The reason you're getting an error using raw_input() is because raw_input() always returns a string; even if a number was entered. So u need to type-cast your value using int().

Code:
>>> value = raw_input('line number')
line number10
>>> value
'10'
>>> int(value)
10
>>> 


Let me know if you have any questions.

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #3  
Old February 8th, 2004, 05:18 PM
Boceifus's Avatar
Boceifus Boceifus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 103 Boceifus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 1 h 17 m 20 sec
Reputation Power: 10
thank you netytan...rappend works like a charm

and thank you for explaining how raw_input always returns strings,even if the input is a integer.

cheers!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > yet another append question

Developer Shed Advertisers and Affiliates



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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap