0

Need help in the below code:

langs=['C','Java','Cobol','Python']

f1=open('a.txt','r')

f2=open('abc.txt','w')

for index in range(len(langs)):

    for line in f1:

        f2.write(line.replace('Frst languag','langs[index]'))

f1.close()

f2.close()

Line #4: Not sure if the syntax is correct, all i want is the loop to run for n number of times where n is no. of values inside the list.

Line 6: All I need is everytime the loop to run and each time langs[0] value #should be written to the file, then langs[1] value and so on...

1
  • 4
    your range(len(langs)) and referring to langs[index] is an anti-pattern in Python. Just use for lang in langs: Commented Sep 16, 2014 at 17:50

1 Answer 1

1
f2.write(line.replace('Frst languag','{}'.format(langs[index]))
Sign up to request clarification or add additional context in comments.

2 Comments

'{}'.format(langs[index]) is the obfuscated version of langs[index].
@Matthias very true. Didn't know if the OP had different intentions when specifically using string interpolation ('language {}'.format(...)

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.