0

First of all I have to say that I am not very experienced in Python programming. I did a lot of data analysis and programming with R for several years. But now I move to Python especially for handling of textfiles.

For what I need your help:

I have a first textfile like to following

My name is Ben.
I am 50 years old.

In a second textfile I have a table with different names and different numbers

Tom, 20
Tim, 30
Tina, 40

I need a loop in which the name 'Ben' is replaced with 'Tom' in the first iteration, 'Tim' in the second and 'Tina' in the last as well as the age of 50 is replaced with 20, 30 and 40. The three new files should be exported as textfile.

In R I would call a search and replace function in a for loop with the rownumber as counter.

In reality my textfile is much more complicated like the little example. Therefore, I am not able to do the seach and replace within R and I want to use Python.

I am able to do the search and replace in Python as well. But I need a hint how I can run such a search and replace in a loop.

Any help is highly welcome.

1
  • So how is the program supposed to handle the input from the first file? Just replace the hard-coded string "Ben" with the new name and the first number found with the new age? The description is not very clear, IMHO. Commented Mar 2, 2012 at 21:40

2 Answers 2

2

There are, oh, so many ways to do it. The absolute simplest one I can think of is the following:

TEMPLATE = "My name is {name}\nI am {age} years old."

for name, age in [("Tom", 20), ("Tim", 30), ("Tina", 40)]:
 print TEMPLATE.format(name=name, age=age)

Output:

My name is Tom
I am 20 years old.
My name is Tim
I am 30 years old.
My name is Tinakughjkjgjkhg
I am 40 years old.

This solution is bound to become complex and ugly as soon as you get more elaborate templates. The first "move" can be to Python template strings (example from documentation):

>>> from string import Template
>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'
>>> d = dict(who='tim')
>>> Template('Give $who $100').substitute(d)
Traceback (most recent call last):
[...]
ValueError: Invalid placeholder in string: line 1, col 10
>>> Template('$who likes $what').substitute(d)
Traceback (most recent call last):
[...]
KeyError: 'what'
>>> Template('$who likes $what').safe_substitute(d)
'tim likes $what'

But the real solution, as far as I'm concerned, is to use a templating engine: Jinja2 will do the trick.

>>> from jinja2 import Template
>>> template = Template('Hello {{ name }}!')
>>> template.render(name='John Doe')
u'Hello John Doe!'
Sign up to request clarification or add additional context in comments.

1 Comment

I think the point is that both datasets come from a text file.
0
txt = open(source).read()
for lig in open(values):
    name, age = lig.split(',')
    rpl = txt.replace('Ben', name.strip())
    rpl = rpl.replace('50', age.strip())
    out = open(name), 'w')
    out.write(rpl)
    out.close()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.