Q:

Replacing a specified line in File

I want to search for a item and then replace it's quantity. Is it possible to do it to a file?
Example of text file:
apples
10.2
banana
20.5
oranges
10

So when I input "banana" I want to be able to update the 20.5 to a different number.

A:

since you haven't posted what you have done so far I can only give you pointers ;
    # open the file -> check out python's open() function and the different modes , w+ opens the file for both writing ad reading
     .... file_handle = open("yourtextfile", "w+")
    # check out readlines() function , it gives you back a list of lines , in the order of from the first line to the last
  ..... the_lines = file_handle.readlines()
  # remove newline '\n' from every element in the list
 ..... new_list = [elem.strip() for elem in the_lines]
    # look for existence of 'banana' in the new_list and going by the structure of the file you just posted edit the value next to the banana i.e 20.5
.... for item in new_list:
        if item == "banana":
           index = new_list.index(item) + 1
           new_list[index] = new_value
           #add the newlines back
           new_string = '\n'.join(new_list)
           file_handle.write(new_string)
           file_handle.close()
           break

