tags:

views:

90

answers:

3

i am using GM862 module and i want to write the cordinates as it is in a file "cordinates.txt" but i get some error, this is the code i wrote:

import MDM

cordlist = []
f = open("cordinates.txt", 'w')

def AcquiredPosition():
  res = MDM.send('AT$GPSACP\r', 0) 
  res = MDM.receive(30)
  if(res.find('OK') != -1):
    tmp = res.split("\r\n")
    res = tmp[1]
    tmp = res.split(" ")
    return tmp[1]
  else:
    return ""

while (1):
    res = MDM.receive(60)
    p = AcquiredPosition()
    cordlist.append(p)
    cordlist.append("\r\n")
    f.writelines(cordlist)

so the problem that the cordinates are being repeted in the list each time the appened happened.

and this is an example of the file content called "cordinates.txt":

160439.246,2612.7206N,05027.6068E,3.0,23.6,2,339.34,4.21,2.27,181109,03     first time
160439.246,2612.7206N,05027.6068E,3.0,23.6,2,339.34,4.21,2.27,181109,03     repeted1
160445.246,2612.7305N,05027.6079E,3.0,23.6,2,161.61,6.37,3.43,181109,03     first time
160439.246,2612.7206N,05027.6068E,3.0,23.6,2,339.34,4.21,2.27,181109,03     repeted2
160445.246,2612.7305N,05027.6079E,3.0,23.6,2,161.61,6.37,3.43,181109,03     repeted1
160451.246,2612.7634N,05027.5939E,3.0,23.6,2,143.18,1.36,0.73,181109,03     first time
160439.246,2612.7206N,05027.6068E,3.0,23.6,2,339.34,4.21,2.27,181109,03     repeted3
160445.246,2612.7305N,05027.6079E,3.0,23.6,2,161.61,6.37,3.43,181109,03
160451.246,2612.7634N,05027.5939E,3.0,23.6,2,143.18,1.36,0.73,181109,03
160458.246,2612.7471N,05027.5979E,3.0,23.6,2,333.97,7.66,4.13,181109,03
160439.246,2612.7206N,05027.6068E,3.0,23.6,2,339.34,4.21,2.27,181109,03     and so on...
160445.246,2612.7305N,05027.6079E,3.0,23.6,2,161.61,6.37,3.43,181109,03
160451.246,2612.7634N,05027.5939E,3.0,23.6,2,143.18,1.36,0.73,181109,03
160458.246,2612.7471N,05027.5979E,3.0,23.6,2,333.97,7.66,4.13,181109,03
160504.246,2612.7496N,05027.5961E,3.0,47.2,3,316.66,3.16,1.70,181109,04
160439.246,2612.7206N,05027.6068E,3.0,23.6,2,339.34,4.21,2.27,181109,03
160445.246,2612.7305N,05027.6079E,3.0,23.6,2,161.61,6.37,3.43,181109,03
160451.246,2612.7634N,05027.5939E,3.0,23.6,2,143.18,1.36,0.73,181109,03
160458.246,2612.7471N,05027.5979E,3.0,23.6,2,333.97,7.66,4.13,181109,03
160504.246,2612.7496N,05027.5961E,3.0,47.2,3,316.66,3.16,1.70,181109,04
160510.000,2612.7446N,05027.5996E,3.0,53.7,3,162.56,0.50,0.27,181109,04

thanks for any help.

+2  A: 

Why not open the file in append mode ('a' instead of 'w') and just writelines to that?

Tom
+3  A: 

You are appending to your list and then writing the full list to the file each time through the loop.

You need to clear down the list in each pass through the loop.

Put cordlist = [] as the first line under while(1)

Gareth Simpson
"""You are appending to your list and then writing the full list to the file each time through the loop. You need to clear down the list in each pass through the loop. Put cordlist = [] as the first line under while(1)""" this solve my problem, thank you very much Gareth Simpson
mahdi86
A: 

Because that's what you've asked it to do. On every iteration, you append an item to the list, then write out all the lines so far. So each time you'll repeat everything you've already done, plus the one new line.

Since your function only returns a single line I don't know why you're bothering with a list at all - just write the result of the function straight to the file.

Daniel Roseman