replacing text in a file, Python

so this piece of code is meant to take a line from a file and replace the certain line from the string with a new word/number, but it doesn't seem to work :(

else:
    with open('newfile', 'r+')as myfile:
           x=input("what would you like to change: \nname \ncolour \nnumber \nenter   option:")
           if x == "name":
               print("your current name is:")
               test_lines = myfile.readlines()
               print(test_lines[0])
               y=input("change name to:")
               content = (y)
               myfile.write(str.replace((test_lines[0]), str(content)))

I get the error message TypeError: replace() takes at least 2 arguments (1 given), i don't know why (content) is not accepted as an argument. This also happens for the code below

if x == "number":
          print ("your current fav. number is:")
          test_lines = myfile.readlines()
          print(test_lines[2])
          number=(int(input("times fav number by a number to get your new number \ne.g 5*2 = 10 \nnew number:")))
          result = (int(test_lines[2])*(number)) 
          print (result)
          myfile.write(str.replace((test_lines[2]), str(result)))





f=open('newfile', 'r')
print("now we will print the file:")
for line in f:
    print (line)
f.close

Answers


replace is a function of a 'str' object.

Sounds like you want to do something like (this is a guess not knowing your inputs)

test_lines[0].replace(test_lines[0],str(content))

I'm not sure what you're attempting to accomplish with the logic in there. looks like you want to remove that line completely and replace it?

also i'm unsure what you are trying to do with

content = (y)

the output of input is a str (which is what you want)

EDIT:

In your specific case (replacing a whole line) i would suggest just reassigning that item in the list. e.g.

test_lines[0] = content

To overwrite the file you will have to truncate it to avoid any race conditions. So once you have made your changes in memory, you should seek to the beginning, and rewrite everything.

# Your logic for replacing the line or desired changes
myfile.seek(0)
for l in test_lines:
    myfile.write("%s\n" % l)
myfile.truncate()

Try this:

test_lines = myfile.readlines()
print(test_lines[0])
y = input("change name to:")
content = str(y)
myfile.write(test_lines[0].replace(test_lines[0], content))

You have no object known purely as str. The method replace() must be called on a string object. You can call it on test_lines[0] which refers to a string object.

However, you may need to change your actual program flow. However, this should circumvent the error.


You need to call it as test_lines[0].replace(test_lines[0],str(content))

Calling help(str.replace) at the interpreter.

replace(...) S.replace(old, new[, count]) -> str

Return a copy of S with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

Couldn't find the docs.


Need Your Help

Working With Stacked UIButtons

iphone objective-c cocoa-touch uibutton touch-event

Hey guys, so I have this search bar in my view along with many buttons below it. So when the search bar is tapped, a keyboard pops up, however I want the user to be able to click anywhere below the

Trim UIImage border

ios objective-c c uiimage cgimage

Here's an example of an image I would like to trim. I want to get rid of the borders around the image (in this case the top and bottom black bars).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.