Now I have this kind of code:
for s,h,v,r in zip(lopullinen, yksilo, osallistumiset, pistemaarat):
tulostuksia.write(str(s) + ";".join(h) + ";" + str(r) + ";" + ";".join(str(v)) + "/7" + "\n")
and it gives this kind of output:
tunnus;nimi;sarja;juoksu-60m;pituushyppy;kuulantyonto;korkeushyppy;aitajuoksu-60m;seivashyppy;juoksu-1000m;kokonaispisteet;lajeja
101;Vertti Veteraaniurheilija;M70;709;651;750;806;850;759;801;5326;7/7
41;Severi Seitsenottelija;M;603;551;600;555;559;655;700;4223;7/7
42;Seppo Seitsenottelija;M;661;750;700;610;505;502;700;4428;7/7
43;Ylermi Yleisurheilija;M;603;601;700;655;661;555;500;4275;7/7
60;K. Keskeyttäjä;M40;603;601;-;-;-;-;-;1204;2/7
61;Yrjänä Yleisurheilija;M40;559;500;650;701;603;655;650;4318;7/7
But I would like to have those numbers to be float-types. Like this:
101;Vertti Veteraaniurheilija;M70;709.0;651.0;750.0;806.0;850.0;759.0;801.0;5326;7/7
Thouse numbers come from the list yksilo, which contains numbers from class. If i change it there to be float(when adding to the list), and from that for loop above ";".join(str(h)). It gives me output where the ";" is between every number(7;0;9;.;0; etc...) And if i don't put the str(h) in there, it gives "TypeError: sequence item 0: expected str instance, float found".
Is there an easy way to get those numbers to be formed 709.0 instead of 709.
Use
'%f' % value
instead of the simple
str(value)
But the str(value)
should already have given you '4.0'
if the value
had been a float in the beginning. So maybe you need to convert that value to a float first:
str(float(value))
Also, if you need to keep the -
values, you will have to check for these as well. So in your example, use
tulostuksia.write(
'%s%s;%s;%s%s' % (
s,
";".join(h),
r,
";".join('-' if vi == 'i' else str(float(vi)) for vi in v),
"/7\n")
If h
is a list [709.0, 651.0]
then str(h)
gives you "[709.0, 651.0]"
and ';'.join(str(h))
, as you've seen, will iterate over the characters in the string, rather than the values in the list.
Instead, try ';'.join(map(str, h))
. This converts each item in the list individually to a string (whether it's a '-'
or a float
), then join
s the resulting iterable/list (depending on Python version) of strings.
Example:
>>> print(";".join(map(str, ["-", 123.4, 567.8, "-", "-", 9.0])))
-;123.4;567.8;-;-;9.0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.