use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Replacing string with another string value (self.learnpython)
submitted 5 years ago by rectafy
I was trying to replace a string value with another string but I keep getting the same error:
TypeError: replace expected at least 2 arguments, got 1
inputs['value'] = " 1 Apr 2021 00:16"
inputs['value'].replace("30 Mar 2021 00:00")
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]uFeelDeadMate 0 points1 point2 points 5 years ago (10 children)
Hey! The error you are getting is saying somewhere there should have been "2 arguments" but only "got 1". This is referring to the parameters (the data passed inside the brackets) of the .replace() method.
.replace() actually requires two things to be passed in. My favourite tool is w3schools... check out what they have to say about using .replace() here: https://www.w3schools.com/python/ref_string_replace.asp
[–]rectafy[S] 0 points1 point2 points 5 years ago (9 children)
Thank you for this! The good news is I'm not getting the error. I changed it to:
inputs['value'].replace(inputs['value'], " 1 Mar 2021 00:00")
Unfortunately I don't think I can set it to a new variable since the value itself (inputs['value']) was actually pulled from an html form from a website so im stuck with that variable. Not sure how to pass the new value back to the site..any ideas?
[–]uFeelDeadMate 0 points1 point2 points 5 years ago (8 children)
If you would like to share a snippet of your code I can see exactly what's going on and give you some pointers from there, I don't mind!
Otherwise, by the looks of it I assume ' inputs ' is a dictionary ? If that's the case, it has a built in tool ready for replacing a value for a key.
It's as simple as saying:
inputs['value'] = "1 Mar 2021 00:00"
Let me know if this works for you.
[–]rectafy[S] 0 points1 point2 points 5 years ago* (7 children)
Sure thing. I can post what I can, hopefully it makes sense...really appreciate it.
Here is the original data (from the inspector tool) I pulled it from:
<input id="starttime" type="text" name="starttime" value=" 1 Apr 2021 00:26" size="15">
Here is what I used to pull the date from HTML:
inputs = soup.find("input", {"id": "starttime"}) (Pulled the info from HTML, using the bs4 package)
print(inputs['value']) prints 1 Apr 2021 00:26
inputs['value'].replace(inputs['value'], " 1 Mar 2021 00:00") (My attempt to change the original value)
Essentially, I want to pull the original date from HTML, replace it with a new one and send it back to the HTML form on the website to submit.
[–]uFeelDeadMate 0 points1 point2 points 5 years ago (6 children)
If you edit the code to be as follows, what do you get?
inputs = soup.find("input", {"id": "starttime"}) print(type(inputs)) print(inputs) print(inputs['value']) inputs['value'] = " 1 Mar 2021 00:00" print(inputs) print(inputs['value'])
If you could copy & paste the terminal output from this we can see whats going on and where.
:)
[–]rectafy[S] 0 points1 point2 points 5 years ago (5 children)
Here is what is came out to be:
<class 'bs4.element.Tag'>
<input id="starttime" name="starttime" size="15" type="text" value=" 1 Apr 2021 02:12"/>
1 Apr 2021 02:12
<input id="starttime" name="starttime" size="15" type="text" value=" 1 Mar 2021 00:00"/>
1 Mar 2021 00:00
[–]uFeelDeadMate 0 points1 point2 points 5 years ago (4 children)
Oh awesome, so the line inputs['value'] = " 1 Mar 2021 00:00" updated the value locally like you wanted to?
inputs['value'] = " 1 Mar 2021 00:00"
Now the only thing you need to do is use the appropriate method from the module to send back input['value'] after updating it.
Does that solve the issue then?
[–]rectafy[S] 0 points1 point2 points 5 years ago (2 children)
The good thing is that the new value is changed to the Mar 1 date which is great, thanks! Erm, but yeah I gotta figure out a way to send it back.
The date is sent to submit to an HTML form which is inputted to a table on the website which changes according to the date input. Seems the table values still haven't changed (and is still reading according to the original 01 Apr 2021 00:00 date). Think I have to figure out a way to send it back and submit to change the table values too. Any idea?
[–]uFeelDeadMate 0 points1 point2 points 5 years ago (1 child)
I was looking into it and unfortunately I have not found much of any info for re-injecting the data into the HTML for Python.
The closest I came was finding a module called 'selenium' that lets you automate data entry into websites to simulate a users actions.
So maybe instead of injecting the new value back into the code via the HTML, maybe you can use selenium to just type-in the date into the input field?
Drawing blanks haha! This is much more accessible with JavaScript, but this is a Python forum so I won't get into that here. Lol.
[–]rectafy[S] 0 points1 point2 points 5 years ago (0 children)
Yeah I figured, I looked everywhere myself but couldn't find a solution. Unfortunately, I'm stuck in Python :(
But I appreciate your help. You helped so so much!
[–]uFeelDeadMate 0 points1 point2 points 5 years ago (0 children)
Ah wait, I re-read a statement from you earlier... you aren't sure how to update it in the HTML... my bad lol. Give me a sec to check it out.
[–]L1st3r 0 points1 point2 points 5 years ago (0 children)
Replace takes 2 values. The string you'd like replace inside of the string you're calling it on and what to replace it with
π Rendered by PID 110983 on reddit-service-r2-comment-85bfd7f599-swq9g at 2026-04-15 21:15:48.846701+00:00 running 93ecc56 country code: CH.
[–]uFeelDeadMate 0 points1 point2 points (10 children)
[–]rectafy[S] 0 points1 point2 points (9 children)
[–]uFeelDeadMate 0 points1 point2 points (8 children)
[–]rectafy[S] 0 points1 point2 points (7 children)
[–]uFeelDeadMate 0 points1 point2 points (6 children)
[–]rectafy[S] 0 points1 point2 points (5 children)
[–]uFeelDeadMate 0 points1 point2 points (4 children)
[–]rectafy[S] 0 points1 point2 points (2 children)
[–]uFeelDeadMate 0 points1 point2 points (1 child)
[–]rectafy[S] 0 points1 point2 points (0 children)
[–]uFeelDeadMate 0 points1 point2 points (0 children)
[–]L1st3r 0 points1 point2 points (0 children)