Python - ayuda al guardar datos con un while en un txt

 
Vista:

ayuda al guardar datos con un while en un txt

Publicado por daniel (1 intervención) el 22/09/2015 04:12:56
Buenas noches tengo un problema con mi codigo cuando guardo los datos de mi sensor solo se guarda el ultimo dato que registro y quisiera guardar todos los datos en el txt espero puedan ayudarme con alguna sugrencia para poder guardar mas de un dato anexo el codigo gracias
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from Adafruit_BMP085 import BMP085
import gps
 
def creartxt():
 
	archivo=open('datos.txt','a')
	archivo.close()
 
while True :
	# Initialise the BMP085 and use STANDARD mode (default value)
	# bmp = BMP085(0x77, debug=True)
	bmp = BMP085(0x77)
 
	# To specify a different operating mode, uncomment one of the following:
	# bmp = BMP085(0x77, 0)  # ULTRALOWPOWER Mode
	# bmp = BMP085(0x77, 1)  # STANDARD Mode
	# bmp = BMP085(0x77, 2)  # HIRES Mode
	# bmp = BMP085(0x77, 3)  # ULTRAHIRES Mode
 
	temp = bmp.readTemperature()
 
	# Read the current barometric pressure level
	pressure = bmp.readPressure()
 
	# To calculate altitude based on an estimated mean sea level pressure
	# (1013.25 hPa) call the function as follows, but this won't be very accurate
	altitude = bmp.readAltitude()
 
	# To specify a more accurate altitude, enter the correct mean sea level
	# pressure level.  For example, if the current pressure level is 1023.50 hPa
	# enter 102350 since we include two decimal places in the integer value
	# altitude = bmp.readAltitude(102350)
 
	print "Temperature: %.2f C" % temp
	print "Pressure:    %.2f hPa" % (pressure / 100.0)
	print "Altitude:    %.2f" % altitude
 
	#crear un scrip en python que nos permita guardar una serie de 
	#cosas por hacer en un archivo txt
	#1.recibir por la terminal las cosas por hacer
 
 
	def agregar_item():
			#1.1 leer lo que el usuario tiene que hacer
			todo_descripcion =  str(temp) + '  ' + str(pressure) + '  '+ str(altitude)
 
			#1.2 escribirlo en un archivo
			#1.2.1 Abrir el archivo
			archivo=open('datos.txt','w')
			#1.2.2 Escribir  el nuevo item
			archivo.write(todo_descripcion)
			#1.2.3 gregar salto
			archivo.write("\n")
			archivo.close()
creartxt()
agregar_item()
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de JanNicZK

ayuda al guardar datos con un while en un txt

Publicado por JanNicZK (3 intervenciones) el 02/10/2015 05:40:27
en la linea 49 cuando llamas a open(archivo w) este abre el archivo en modo escritura y comiensa a escribir desde la primer linea.
te recomiendo abrir el archivo (en modo lectura) copiar los datos cerrar el archivo, abrir el archivo (en modo escritura) escribir los datos, escribir el dato a agregar finalmente cerrar el archivo.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar