1

I've been working on a project where I open and read a file and replace its text with input, this is what I have been trying to do to put certain words in,

for i in range(0, length):
    finallib = str(picks[choice]) % (picked[i])

where length is the length - 1 of the list 'picked' and picked is a list of words. 'picks[choice]' is the selection of text from the file that the user chooses to put new words into. Within the file, The text goes like this 'file text file text file text %s file text file text file text', and I get the error that there are not enough arguments for format string. How can I get it to replace what I want it to?

P.S. the substitution doesn't go into another file or the same file, it is put into a variable that is displayed after it is calculated.

1 Answer 1

2

Why not just use format()?

for i in range(length): # range automatically starts at 0
    finallib = '{} % {}'.format(picks[choice],picked[i])

Or cleaner (list comprehension):

finallib = ['{} % {}'.format(picks[choice],picked[i]) for i in range(length)]
Sign up to request clarification or add additional context in comments.

8 Comments

so I tried that exact phrasing and it returned the file choice without substitutions included, right now the spots reserved for inputs are marked with '%s', should they be marked with something else or is the code missing that piece somewhere? When I tried the second option it printed the file selection multiple times.
'{ }".format(x) places the variable x in the { } inside the string (formed by the quotes). If you have many variables just use { } where ever they need to go. In the above example there are 2 variables and each is placed around the % symbol in the string at the order they are written in the format(). But what you said should also work - namely, using %s. Without seeing the rest of the code there is not much I can suggest.
so I tried to replace %s with {} and I tried to just use %s where you had {} in the example, when using the first method output was only '%s % %s' and when I tried the second it output the file selection followed by % {}. I do not understand this premise very well
Read about format() online: geeksforgeeks.org/python-format-function
I have read the article, and I now don't think its the command screwing it but the way it gets where it's going. the way I get the string is by access from a file, and I think that that is screwing up the way that it reads either %s or {}. The error I am now getting is 'tuple range out of index' or something like that, and from what I know thats what happens when there aren't enough arguments. I know I have the right amount of arguments but can't get it to replace each sub with an element in the list. I believe it wants to replace the first sub with the entire list. how do I overcome this?
|

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.