How to put together datas into a file?
up vote
-2
down vote
favorite
I would like to collect different type of datas into a file. Here is a part of the code.
val = str(float(data[-1]))
val_dB = float(val)
val_dB = math.log(val_dB, 10) * 10
myfile = open('../../../MLI_values/mli_value.txt', 'a')
myfile.write(date_ID + " " + val + val_dB + "n")
myfile.close()
But it gives back an error:
myfile.write(date_ID + " " + val + val_dB + "n")
TypeError: cannot concatenate 'str' and 'float' objects
How can I solve it to put them together? (into columns) into a file?
python
add a comment |
up vote
-2
down vote
favorite
I would like to collect different type of datas into a file. Here is a part of the code.
val = str(float(data[-1]))
val_dB = float(val)
val_dB = math.log(val_dB, 10) * 10
myfile = open('../../../MLI_values/mli_value.txt', 'a')
myfile.write(date_ID + " " + val + val_dB + "n")
myfile.close()
But it gives back an error:
myfile.write(date_ID + " " + val + val_dB + "n")
TypeError: cannot concatenate 'str' and 'float' objects
How can I solve it to put them together? (into columns) into a file?
python
3
Possible duplicate of Cannot concatenate 'str' and 'float' objects?
– Georgy
Nov 7 at 9:05
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I would like to collect different type of datas into a file. Here is a part of the code.
val = str(float(data[-1]))
val_dB = float(val)
val_dB = math.log(val_dB, 10) * 10
myfile = open('../../../MLI_values/mli_value.txt', 'a')
myfile.write(date_ID + " " + val + val_dB + "n")
myfile.close()
But it gives back an error:
myfile.write(date_ID + " " + val + val_dB + "n")
TypeError: cannot concatenate 'str' and 'float' objects
How can I solve it to put them together? (into columns) into a file?
python
I would like to collect different type of datas into a file. Here is a part of the code.
val = str(float(data[-1]))
val_dB = float(val)
val_dB = math.log(val_dB, 10) * 10
myfile = open('../../../MLI_values/mli_value.txt', 'a')
myfile.write(date_ID + " " + val + val_dB + "n")
myfile.close()
But it gives back an error:
myfile.write(date_ID + " " + val + val_dB + "n")
TypeError: cannot concatenate 'str' and 'float' objects
How can I solve it to put them together? (into columns) into a file?
python
python
edited Nov 7 at 9:04
Nick Parsons
2,2062518
2,2062518
asked Nov 7 at 9:01
Krsztr
12
12
3
Possible duplicate of Cannot concatenate 'str' and 'float' objects?
– Georgy
Nov 7 at 9:05
add a comment |
3
Possible duplicate of Cannot concatenate 'str' and 'float' objects?
– Georgy
Nov 7 at 9:05
3
3
Possible duplicate of Cannot concatenate 'str' and 'float' objects?
– Georgy
Nov 7 at 9:05
Possible duplicate of Cannot concatenate 'str' and 'float' objects?
– Georgy
Nov 7 at 9:05
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
Change:
myfile.write(date_ID + " " + val + val_dB + "n")
to:
myfile.write(date_ID + " " + val + " " + str(val_dB) + "n")
add a comment |
up vote
0
down vote
Apparently your val_dB is a float. If you want to write it you need to convert it into a string str(val_dB)
However, you can do it implicitely with format
method.
myfile.write("{} {} {}n".format(date_ID, val, val_dB))
https://www.programiz.com/python-programming/methods/string/format
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Change:
myfile.write(date_ID + " " + val + val_dB + "n")
to:
myfile.write(date_ID + " " + val + " " + str(val_dB) + "n")
add a comment |
up vote
0
down vote
Change:
myfile.write(date_ID + " " + val + val_dB + "n")
to:
myfile.write(date_ID + " " + val + " " + str(val_dB) + "n")
add a comment |
up vote
0
down vote
up vote
0
down vote
Change:
myfile.write(date_ID + " " + val + val_dB + "n")
to:
myfile.write(date_ID + " " + val + " " + str(val_dB) + "n")
Change:
myfile.write(date_ID + " " + val + val_dB + "n")
to:
myfile.write(date_ID + " " + val + " " + str(val_dB) + "n")
answered Nov 7 at 9:04
Paul R
174k24294452
174k24294452
add a comment |
add a comment |
up vote
0
down vote
Apparently your val_dB is a float. If you want to write it you need to convert it into a string str(val_dB)
However, you can do it implicitely with format
method.
myfile.write("{} {} {}n".format(date_ID, val, val_dB))
https://www.programiz.com/python-programming/methods/string/format
add a comment |
up vote
0
down vote
Apparently your val_dB is a float. If you want to write it you need to convert it into a string str(val_dB)
However, you can do it implicitely with format
method.
myfile.write("{} {} {}n".format(date_ID, val, val_dB))
https://www.programiz.com/python-programming/methods/string/format
add a comment |
up vote
0
down vote
up vote
0
down vote
Apparently your val_dB is a float. If you want to write it you need to convert it into a string str(val_dB)
However, you can do it implicitely with format
method.
myfile.write("{} {} {}n".format(date_ID, val, val_dB))
https://www.programiz.com/python-programming/methods/string/format
Apparently your val_dB is a float. If you want to write it you need to convert it into a string str(val_dB)
However, you can do it implicitely with format
method.
myfile.write("{} {} {}n".format(date_ID, val, val_dB))
https://www.programiz.com/python-programming/methods/string/format
answered Nov 7 at 9:07
BlueSheepToken
500110
500110
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186244%2fhow-to-put-together-datas-into-a-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Possible duplicate of Cannot concatenate 'str' and 'float' objects?
– Georgy
Nov 7 at 9:05