Python Forum
"." to "," when writing to .txt
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"." to "," when writing to .txt
#1
import time
from datetime import datetime

DS18B20="/sys/bus/w1/devices/28-01204febab44/w1_slave"

while True:
   
   now = datetime.now()
   dt_string = now.strftime('%d/%m/%Y   %H:%M:%S ')
   
   f = open(DS18B20, "r")
   data = f.read()
   f.close()
   (discard, sep, reading) = data.partition(" t=")
   t = float(reading) / 1000.0
   
   print dt_string, '{:.1f}*C' .format(t)
   time.sleep(4) 

   f = open("data.txt", "a+")
   print >> f, dt_string, '{:.1f}*C%' .format(t)
Google - non
Dutch forums - non
US general forums - non
General tutorials and etc. - non (Yes, without my own code and in basic it works)
Time spend with experimenting, maybe over 10.00 hours.

Goal: Change my output print >> f with replace "." tot "," while writing to .txt.
It's my first time writing code and spent a lot of time and now I'm stuck at this last peace. I need to tackle this problem before I can go further in my project with writing more code for a lot of different sensors. The only thing I can think of now is making a second .txt en reed out the first data.txt to change the . to , only I think that that is not the perfect solution and is somehow not logic to me.

Asking: Can somebody tell me if it is possible and if it is, give me some hints how to figure this out. If it is not possible than it would be ridicules to spend many hours extra. It is just agonizing me that I can't figure this out....

With kind regards,
Lullaby (Dutchman).
Reply
#2
>>> s = "hello.this.is.an.example"
>>> s.replace('.', ',')
'hello,this,is,an,example'
>>> 
Also you shouldn't be using Python 2, especially if you are a Python newbie.
Reply
#3
I'm out of options and exhausted by now.... Can someone give me the answer.....

After this I need to change to Python 3 Blush Never thought about something like that, just wanne learn python so started without knowledge. So yes, I am a uber newbie Wall Big Grin

Edit: Meanwhile still trying to figure this out (I need to try my best). 22.20 now in the Netherlands I will fix this!
Reply
#4
What's unclear in my answer? Doesn't it show how to replace "." with "," ?
Reply
#5
(Jun-12-2021, 09:18 PM)Gribouillis Wrote: What's unclear in my answer? Doesn't it show how to replace "." with "," ?

Yes it is clear and should be really easy (it works fine without my own code) Only in my own code it won't work whatever option I'm trying. So I am doing something horrible wrong and don't understand why it won't work. It should be so simple...

I'm sorry that I don't know...
Reply
#6
(Jun-12-2021, 09:45 PM)Lullaby Wrote: So I am doing something horrible wrong and don't understand why it won't work. It should be so simple...

Please show your entire code with how you're trying to do the string replacement. It's hard to know what you might be doing wrong without seeing the code.
Lullaby likes this post
Reply
#7
Don't know witch one to give Big Grin

import time
from datetime import datetime

DS18B20="/sys/bus/w1/devices/28-01204febab44/w1_slave"

while True:
   now = datetime.now()
   dt_string = now.strftime('%d/%m/%Y   %H:%M ')
   
   
   
   f = open(DS18B20, "r")
   data = f.read()
   f.close()
   (discard, sep, reading) = data.partition(" t=")
   t = float(reading) / 1000.0
   print dt_string, '{:.1f}*C' .format(t)
   
   
   #nothing wrong till now and a fine .txt
   d = open ("data.txt", "a+")
   print >> d, dt_string,'{:.1f}*C' .format(t)
   
   #Basic code:
   #s = "banana. kiwi. bla"
   #s.replace (".", ",")
   # print s
   #outcome: no dots 
   
   #option 1.
   #d = ("./data.txt", "a+")
   #d.replace (".", ",")  
   #print d
   
   #Option 2.
   # d = open ("data.txt", "a+")
   # d = [s.replace(".", ",") for s in d]
   # print d
   
   #Option 3.
   # d = [","]
   # d_new = [s.replace(".", ",") for s in d_new]
   
   
   #and a hole lot of other combinations with {:.1f}, t, f, new file's and etc.  (like 50 or more...) 
   
   
   time.sleep(4)
I have been testing so many options that i dont know what to test anymore.... Been trying to understand all the erros and lokking on the internet for sollutions and nothing seems to work... I am surtan that it is possible... But i just dont know...
Reply
#8
Try this perhaps
   
print >> d, dt_string,'{:.1f}*C' .format(t).replace('.', ',')
Lullaby likes this post
Reply
#9
Evil

I have been trying that.... only with a "," in between format and replace.... (over 14 hours and now it works....)

print >> d, dt_string,'{:.1f}*C' .format(t), .replace('.', ',')
Heart
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020