0

I am trying to operate some file like this:

*sline
1, 1.0, 2.23
2, 1.0, 9.98
3, 2.0, 10.00
*eline

Now I have a list, which contain data like this:

datalist = [[1,1.0,2.0],[3,2.0,2.0]]

I want to put the value which belong to datalist to replace the value in the file by id(datalist[][0]), like:

*sline
1, 1.0, 2.0
2, 1.0, 9.98
3, 2.0, 2.0
*eline

I am try to utilse:

o = open("file","a") 
for line in open("trychange.txt"):
   line = line.replace("1, 1.0, 2.23","1, 1.0, 2.0")
   line = line.replace("3, 2.0, 10.00","3, 2.0, 2.0")
   o.write(line + "\n") 
o.close()

but it doesn't work, it just add new values but didn't change the old one. how can I deal with this? Thanks for your helping

GuangWu
  • 143
  • 1
  • 1
  • 6
  • There's probably a better way to try to do what you're describing than the hard coded replaces you're doing, but I suspect the adding new values problem is because you're opening `file` with the `a` flag to open it in append mode, so every time you run it you'll be adding all the output again – Eric Renouf Aug 30 '15 at 18:27
  • 2
    Use `o = open("file","w") ` instead of `o = open("file","a") ` as `a` appends to the previous value and does not erase the original contents – Bhargav Rao Aug 30 '15 at 18:33

1 Answers1

0
  1. Open the file with "r" mode and read all the contents.

    • While you are reading in the contents of the file (strings) convert the lines into easy-to-process data structures (e.g.: list of integers...)
  2. Process the previous read contents in memory.

    • Note that while you read in the file (or after reading the file) you can build up several auxiliary data structures that support you in carrying out the operations on your data. For example in your case the in-memory data structure should include a list that contains the data for each line, and also a dictionary that helps you to lookup lines using the two numbers. See the example below.
  3. open the file again with "w" mode and overwrite the previous file with the new data.

    • You can convert the in-memory data into string line-by-line.
    • If you want a "safe" overwrite then first create a temporary file, write out the new data, and rename the temp file to the original one. Note that on unix this rename technique can be used to "atomically change the file contents". This way you shouldn't end up with a partially saved file.

Example in memory data structure:

lines = [
    [1, 1.0, 2.23],
    [2, 1.0, 9.98],
    [3, 2.0, 10.00],
]

key_to_lines_index = {
    (1, 1.0): 0,  # 0 is an index to the lines array
    (2, 1.0): 1,  # 1 is an index to the lines array
    (3, 2.0): 2,  # 2 is an index to the lines array
}

With the above in memory data structure you can apply the datalist items easily. For each datalist item you can use the key_to_lines_index dictionary to lookup which line you have to modify and then you modify the corresponding line in the lines array. When you finished you simply write out the modified file from the lines array.

If this help isn't enough for you to get the job done then you have problem understanding fundamental principles of programming so the right place to continue is something like this: https://wiki.python.org/moin/BeginnersGuide/Programmers

pasztorpisti
  • 3,760
  • 1
  • 16
  • 26