text manipulation

J

Johhny

Hello,

I am trying to write a script in python (to replace a perl script with
limited functionality). Now I have some issues. Currently I am using
the perl to load the file then regex parse certain lines to remove
characters (uncomment lines and change variables). I would like to take
that into the python script. I have had a look at the module "string"
and I dont think its what Im looking for.

Here is an example of some text I would like to manipulate

#comment here
#user_defined_variable = no
#

I would like to make that

#comment here
user_defined_variable = yes
#

With perl/sed Its very easy, However Im having issues to do it in
python. Any advice would be great.

Regards,

Johhny.
 
I

Isaac T Alston

Johhny said:
Any advice would be great.


Have you had a look at Python's re module? If you've not, I suggest you do
so - it contains all of Python's regex tools which you'll need.

Regards,
 
M

Martin Franklin

Johhny said:
Hello,

I am trying to write a script in python (to replace a perl script with
limited functionality). Now I have some issues. Currently I am using
the perl to load the file then regex parse certain lines to remove
characters (uncomment lines and change variables). I would like to take
that into the python script. I have had a look at the module "string"
and I dont think its what Im looking for.

Here is an example of some text I would like to manipulate

#comment here
#user_defined_variable = no
#

I would like to make that

#comment here
user_defined_variable = yes
#

With perl/sed Its very easy, However Im having issues to do it in
python. Any advice would be great.

Regards,

Johhny.


forget regular expressions for this job... strings have methods in
python so for example:


for line in file:
if line.startswith("#user_defined_variable = no"):
line.replace("#user_defined_variable = no",
"user_defined_variable = yes")

.... continue processing file / writing out stuff as you go


Cheers
Martin
 
J

Juho Schultz

Johhny said:
Hello,

I am trying to write a script in python (to replace a perl script with
limited functionality). Now I have some issues. Currently I am using
the perl to load the file then regex parse certain lines to remove
characters (uncomment lines and change variables). I would like to take
that into the python script. I have had a look at the module "string"
and I dont think its what Im looking for.

Here is an example of some text I would like to manipulate

#comment here
#user_defined_variable = no
#

I would like to make that

#comment here
user_defined_variable = yes
#

With perl/sed Its very easy, However Im having issues to do it in
python. Any advice would be great.

Regards,

Johhny.

Have you also looked at the built-in string methods?


The following script is probably something you
could start with - it just uncomments lines
and replaces values of some variables.

# dictionary of the variable names
# contains the replacement values
newvalues = {'userdefinedvar1':'yes',
'userdefinedvar2':'123.654.345.234'}

# read file to input
output = []
for line in input:
# check if it is a comment with "="
if line.startswith('#') and ('=' in line):
name, oldvalue = (line.lstrip('#')).split('=')
# line.lstrip removes leading comments,
# split('=') finds the assignment
if newvalues.has_key(name):
# replace line
output.append("%s = %s" % (name, newvalue[name]))
else:
# or just uncomment
output.append("%s = %s" % (name, oldvalue))
else:
output.append(line)

# write output to file
 
M

Martin Franklin

Martin said:
forget regular expressions for this job... strings have methods in
python so for example:


for line in file:
if line.startswith("#user_defined_variable = no"):
line.replace("#user_defined_variable = no",
"user_defined_variable = yes")

... continue processing file / writing out stuff as you go


Cheers
Martin




whoops forgot string methods return the new string.....

for line in file:
if line.startswith("#user_defined_variable = no"):
line = line.replace("#user_defined_variable = no",
"user_defined_variable = yes")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top