[Tutor] changin two lines in a single python script

bob gailer bgailer at gmail.com
Wed May 21 19:31:45 CEST 2008


1) When starting a new topic start a new email rather than replying to 
another unrelated post.
2)
jatinder Singh wrote:
> Hi .
>
> 	I am trying to change two lines in a single file (etc/php.ini) but my
> script is not doing this in one time .Please hav a look
>
> import re
> import sys
> import os
>
> def re_new(pat, rep, s):
>          print re.sub(pat, rep, s)
>        
> _stext_1 = 'memory_limit...M'            #first word to search
> _rtext_1 = 'memory_limit = 512M  ;'    #to be replaced with first word
> _stext_2 = 'upload_max_filesize.*M'      #Second word
> _rtext_2 = 'upload_max_filesize = 9M  ;'         #to replace with second
> word
> s = open("/home/ccuser/temp1.txt").readlines()
> for record in s:
>         re_new(_stext_1,_rtext_1,record)  #if found first then replaceit
>         re_new(_stext_2,_rtext_2,record)  #else if found second then
> replace that
>
> Will you please help me to correct this script so as to both of the
> changes occure at a singlr time
>
> Thanks for past, now and future
>
> Jatin
>
>
> On Wed, 2008-05-21 at 16:29 +0200, tutor-request at python.org wrote:
>   
>> Send Tutor mailing list submissions to
>> 	tutor at python.org
>>
>> To subscribe or unsubscribe via the World Wide Web, visit
>> 	http://mail.python.org/mailman/listinfo/tutor
>> or, via email, send a message with subject or body 'help' to
>> 	tutor-request at python.org
>>
>> You can reach the person managing the list at
>> 	tutor-owner at python.org
>>
>> When replying, please edit your Subject line so it is more specific
>> than "Re: Contents of Tutor digest..."
>>
>>
>> Today's Topics:
>>
>>    1. Re: String Replacement question (Wolfram Kraus)
>>    2. python investment scheme assistance (Binish Huma Qadir)
>>    3. Re: String Replacement question (Kent Johnson)
>>    4. Re: String Replacement question (Moishy Gluck)
>>    5. Re: python investment scheme assistance (Kent Johnson)
>>    6. String Replacement Question (Kinuthia Muchane)
>>    7. Re: String Replacement Question (Moishy Gluck)
>>
>>
>> ----------------------------------------------------------------------
>>
>> Message: 1
>> Date: Wed, 21 May 2008 12:11:28 +0200
>> From: Wolfram Kraus <wolfram.kraus at fen-net.de>
>> Subject: Re: [Tutor] String Replacement question
>> To: tutor at python.org
>> Message-ID: <g10siv$psb$1 at ger.gmane.org>
>> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>>
>> Am 21.05.2008 11:35, Faheem schrieb:
>>     
>>> Hi all,
>>>  How do I replace the same value multiple times without repeating the
>>> same variable name/value repeatedly?
>>> for ex.  
>>>
>>>  some = 'thing' 
>>>  print '%s %s %s %s' % (some,some,some,some)
>>>
>>> in this case, my question is how do i replace "% (some,some,some)" with
>>> something more concise?
>>>
>>> thanks in advance,
>>> Faheem
>>>       
>> Hi!
>>
>> Two possible solutions:
>>
>> print "%s %s %s %s" % (tuple([some]*4))
>>
>> print " ".join([some for x in range(4)])
>>
>> HTH,
>> Wolfram
>>
>>
>>
>> ------------------------------
>>
>> Message: 2
>> Date: Wed, 21 May 2008 19:35:01 +1000 (EST)
>> From: Binish Huma Qadir <b.qadir at ugrad.unimelb.edu.au>
>> Subject: [Tutor] python investment scheme assistance
>> To: tutor at python.org
>> Message-ID:
>> 	<3049.59.101.135.171.1211362501.squirrel at webmail.student.unimelb.edu.au>
>> 	
>> Content-Type: text/plain; charset=UTF-8
>>
>> Hi,
>>
>> I'm doing a Investment implementation project where i need to use the
>> following data:
>> http://app.lms.unimelb.edu.au/bbcswebdav/courses/600151_2008_1/datasets/finance/asx_large.csv
>>
>> I've been instructed to design an investment scheme in python.
>> The investment scheme i have chosen is based on investing in 3companies
>> with the highest dividend yield and lowest debt equity ratio.
>>
>> The problem is that i dont know how to set up a python code that gives me
>> the top 3 companies with the above characterisics.
>>
>> Your help would be much appreciated.
>> Thank you very much for your time.
>> Regards,
>> Binish
>>
>>
>>
>> ------------------------------
>>
>> Message: 3
>> Date: Wed, 21 May 2008 06:36:30 -0400
>> From: "Kent Johnson" <kent37 at tds.net>
>> Subject: Re: [Tutor] String Replacement question
>> To: Faheem <faheem at atlantiscomputing.com>
>> Cc: tutor at python.org
>> Message-ID:
>> 	<1c2a2c590805210336h71fdc349r803b7ba076cb5f88 at mail.gmail.com>
>> Content-Type: text/plain; charset=ISO-8859-1
>>
>> On Wed, May 21, 2008 at 5:35 AM, Faheem <faheem at atlantiscomputing.com> wrote:
>>     
>>> Hi all,
>>>  How do I replace the same value multiple times without repeating the
>>> same variable name/value repeatedly?
>>> for ex.
>>>
>>>  some = 'thing'
>>>  print '%s %s %s %s' % (some,some,some,some)
>>>       
>> You can use named parameters, which moves the repetition to the format string:
>>
>> In [24]: print '%(some)s %(some)s %(some)s %(some)s' % (dict(some=some))
>> thing thing thing thing
>>
>> With multiple values, a common trick is to pass vars() or locals() as
>> the dict, giving the format access to all defined variables:
>>
>> In [27]: a=1
>>
>> In [28]: b=2
>>
>> In [29]: print '%(a)s %(b)s' % vars()
>> 1 2
>>
>> If you literally need to repeat as in your example, you could do this:
>>
>> In [26]: print '%s %s %s %s' % (4*(some,))
>> thing thing thing thing
>>
>> Kent
>>
>>
>> ------------------------------
>>
>> Message: 4
>> Date: Wed, 21 May 2008 07:04:16 -0400
>> From: "Moishy Gluck" <moishyyehuda at gmail.com>
>> Subject: Re: [Tutor] String Replacement question
>> To: tutor at python.org
>> Message-ID:
>> 	<187d38240805210404r1a6877edp610c1ef3372e44a0 at mail.gmail.com>
>> Content-Type: text/plain; charset="iso-8859-1"
>>
>> Some other solutions might be
>>
>>     
>>>>> animal = 'cat'
>>>>> " ".join((animal, ) * 4)
>>>>>           
>> 'cat cat cat cat'
>>     
>>>>> animal = 'cat'
>>>>> print " ".join((animal, ) * 4)
>>>>>           
>> cat cat cat cat
>>     
>>>>> #If you need the string injected into another string
>>>>> print "My %s's name is ginger." % (" ".join((animal,) * 4))
>>>>>           
>> My cat cat cat cat's name is ginger.
>>     
>> On Wed, May 21, 2008 at 6:36 AM, Kent Johnson <kent37 at tds.net> wrote:
>>
>>     
>>> On Wed, May 21, 2008 at 5:35 AM, Faheem <faheem at atlantiscomputing.com>
>>> wrote:
>>>       
>>>> Hi all,
>>>>  How do I replace the same value multiple times without repeating the
>>>> same variable name/value repeatedly?
>>>> for ex.
>>>>
>>>>  some = 'thing'
>>>>  print '%s %s %s %s' % (some,some,some,some)
>>>>         
>>> You can use named parameters, which moves the repetition to the format
>>> string:
>>>
>>> In [24]: print '%(some)s %(some)s %(some)s %(some)s' % (dict(some=some))
>>> thing thing thing thing
>>>
>>> With multiple values, a common trick is to pass vars() or locals() as
>>> the dict, giving the format access to all defined variables:
>>>
>>> In [27]: a=1
>>>
>>> In [28]: b=2
>>>
>>> In [29]: print '%(a)s %(b)s' % vars()
>>> 1 2
>>>
>>> If you literally need to repeat as in your example, you could do this:
>>>
>>> In [26]: print '%s %s %s %s' % (4*(some,))
>>> thing thing thing thing
>>>
>>> Kent
>>> _______________________________________________
>>> Tutor maillist  -  Tutor at python.org
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>>>       
>> -------------- next part --------------
>> An HTML attachment was scrubbed...
>> URL: <http://mail.python.org/pipermail/tutor/attachments/20080521/4ac3ef9d/attachment-0001.htm>
>>
>> ------------------------------
>>
>> Message: 5
>> Date: Wed, 21 May 2008 08:45:10 -0400
>> From: "Kent Johnson" <kent37 at tds.net>
>> Subject: Re: [Tutor] python investment scheme assistance
>> To: "Binish Huma Qadir" <b.qadir at ugrad.unimelb.edu.au>
>> Cc: tutor at python.org
>> Message-ID:
>> 	<1c2a2c590805210545g4a4c188cu70aca0f4c406508 at mail.gmail.com>
>> Content-Type: text/plain; charset=ISO-8859-1
>>
>> On Wed, May 21, 2008 at 5:35 AM, Binish Huma Qadir
>> <b.qadir at ugrad.unimelb.edu.au> wrote:
>>     
>>> Hi,
>>>
>>> I'm doing a Investment implementation project where i need to use the
>>> following data:
>>> http://app.lms.unimelb.edu.au/bbcswebdav/courses/600151_2008_1/datasets/finance/asx_large.csv
>>>
>>> I've been instructed to design an investment scheme in python.
>>> The investment scheme i have chosen is based on investing in 3companies
>>> with the highest dividend yield and lowest debt equity ratio.
>>>       
>> This sounds like a homework problem; we try to avoid doing homework.
>> We will help you with specific questions.
>>
>> Do you know how to read the file? Maybe you could start with a program
>> that just reads and prints the csv file. The csv module can help with
>> that. Then compute and print the measures you need; finally pick out
>> the top three.
>>
>> Try to write some code and ask here when you have trouble.
>>
>> Kent
>>
>>
>> ------------------------------
>>
>> Message: 6
>> Date: Wed, 21 May 2008 16:42:11 +0300
>> From: Kinuthia Muchane <muchanek at gmail.com>
>> Subject: [Tutor] String Replacement Question
>> To: tutor at python.org
>> Message-ID: <483426B3.5080309 at gmail.com>
>> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>>
>>     
>>>>> st = "String"
>>>>> print "%s " %st*3
>>>>>           
>> String String String
>>     
>> Does this help?
>>
>> Kinuthia...
>>
>> -----------------------------
>>
>> Message: 6
>> Date: Wed, 21 May 2008 15:05:21 +0530
>> From: Faheem <faheem at atlantiscomputing.com>
>> Subject: [Tutor] String Replacement question
>> To: tutor at python.org
>> Message-ID: <20080521150521.604437d8 at atlantiscomputing.com>
>> Content-Type: text/plain; charset=US-ASCII
>>
>> Hi all,
>>   How do I replace the same value multiple times without repeating the
>> same variable name/value repeatedly?
>> for ex.
>>
>>   some = 'thing'
>>   print '%s %s %s %s' % (some,some,some,some)
>>
>> in this case, my question is how do i replace "% (some,some,some)" with
>> something more concise?
>>
>> thanks in advance,
>> Faheem
>>
>>
>>
>> ------------------------------
>>
>> Message: 7
>> Date: Wed, 21 May 2008 10:29:41 -0400
>> From: "Moishy Gluck" <moishyyehuda at gmail.com>
>> Subject: Re: [Tutor] String Replacement Question
>> To: tutor at python.org
>> Message-ID:
>> 	<187d38240805210729r29f225bbyae313adb3f3423b5 at mail.gmail.com>
>> Content-Type: text/plain; charset="iso-8859-1"
>>
>> On Wed, May 21, 2008 at 10:29 AM, Moishy Gluck <moishyyehuda at gmail.com>
>> wrote:
>>
>>     
>>>>>> "%s " %st*3
>>>>>>             
>>> 'String String String '
>>>                             ^
>>> If you look closely at the end of the string there is an extra space.
>>>
>>>       
>>>>>> " ".join((st, )*3)
>>>>>>             
>>> 'String String String'
>>>                            ^
>>> No extra space.
>>>
>>>
>>> On Wed, May 21, 2008 at 9:42 AM, Kinuthia Muchane <muchanek at gmail.com>
>>> wrote:
>>>
>>>       
>>>>  st = "String"
>>>>         
>>>>>>> print "%s " %st*3
>>>>>>>
>>>>>>>               
>>>>>> String String String
>>>>>>             
>>>> Does this help?
>>>>
>>>> Kinuthia...
>>>>
>>>> -----------------------------
>>>>
>>>> Message: 6
>>>> Date: Wed, 21 May 2008 15:05:21 +0530
>>>> From: Faheem <faheem at atlantiscomputing.com>
>>>> Subject: [Tutor] String Replacement question
>>>> To: tutor at python.org
>>>> Message-ID: <20080521150521.604437d8 at atlantiscomputing.com>
>>>> Content-Type: text/plain; charset=US-ASCII
>>>>
>>>> Hi all,
>>>>  How do I replace the same value multiple times without repeating the
>>>> same variable name/value repeatedly?
>>>> for ex.
>>>>
>>>>  some = 'thing'
>>>>  print '%s %s %s %s' % (some,some,some,some)
>>>>
>>>> in this case, my question is how do i replace "% (some,some,some)" with
>>>> something more concise?
>>>>
>>>> thanks in advance,
>>>> Faheem
>>>>
>>>> _______________________________________________
>>>> Tutor maillist  -  Tutor at python.org
>>>> http://mail.python.org/mailman/listinfo/tutor
>>>>
>>>>         
>>>       
>> -------------- next part --------------
>> An HTML attachment was scrubbed...
>> URL: <http://mail.python.org/pipermail/tutor/attachments/20080521/c486b262/attachment.htm>
>>
>> ------------------------------
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>> End of Tutor Digest, Vol 51, Issue 44
>> *************************************
>>     


-- 
Bob Gailer
919-636-4239 Chapel Hill, NC



More information about the Tutor mailing list