Posts: 5
Threads: 1
Joined: Jun 2021
Jun-11-2021, 11:34 PM
(This post was last modified: Jun-11-2021, 11:34 PM by Lullaby.)
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).
Posts: 4,758
Threads: 75
Joined: Jan 2018
>>> 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.
Posts: 5
Threads: 1
Joined: Jun 2021
Jun-12-2021, 08:22 PM
(This post was last modified: Jun-12-2021, 08:22 PM by Lullaby.)
I'm out of options and exhausted by now.... Can someone give me the answer.....
After this I need to change to Python 3  Never thought about something like that, just wanne learn python so started without knowledge. So yes, I am a uber newbie
Edit: Meanwhile still trying to figure this out (I need to try my best). 22.20 now in the Netherlands I will fix this!
Posts: 4,758
Threads: 75
Joined: Jan 2018
What's unclear in my answer? Doesn't it show how to replace "." with "," ?
Posts: 5
Threads: 1
Joined: Jun 2021
(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...
Posts: 1,582
Threads: 3
Joined: Mar 2020
(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.
Posts: 5
Threads: 1
Joined: Jun 2021
Jun-12-2021, 10:47 PM
(This post was last modified: Jun-12-2021, 10:47 PM by Lullaby.)
Don't know witch one to give
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...
Posts: 4,758
Threads: 75
Joined: Jan 2018
Try this perhaps
print >> d, dt_string,'{:.1f}*C' .format(t).replace('.', ',')
Posts: 5
Threads: 1
Joined: Jun 2021
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('.', ',')
|