Hi,
I have the following row of data.
I/P:
20200107 1180.3489999999999 223.56720000000001 191.21019999999999
I would like to write this data to the file with two decimal places.
O/P:
20200107 1180.35 223.57 191.21
What is the syntax of it?.
Thanks
PSB
13 1892
Hi,
I have the following row of data.
I/P:
20200107 1180.3489999999999 223.56720000000001 191.21019999999999
I would like to write this data to the file with two decimal places.
O/P:
20200107 1180.35 223.57 191.21
What is the syntax of it?.
Thanks
PSB
-
>>> argStr = "20200107 1180.3489999999999 223.56720000000001 191.21019999999999"
-
>>> argList = argStr.split()
-
>>> a = int(argList[0])
-
>>> b, c, d = (float(x) for x in argList[1:])
-
>>> "%d %.2f %.2f %.2f" %(a, b, c, d)
-
'20200107 1180.35 223.57 191.21'
-
>>>
-
a = "20200107 1180.3489999999999 223.56720000000001 191.21019999999999"
-
print a.split()[0] ,
-
for i in [ "%.2f" % float(i) for i in a.split()[1:] ]: print i,
-
-
a = "20200107 1180.3489999999999 223.56720000000001 191.21019999999999"
-
print a.split()[0] ,
-
for i in [ "%.2f" % float(i) for i in a.split()[1:] ]: print i,
-
So, is this a list comprehension to avoid using a generator due to the newness of generators?
So, is this a list comprehension to avoid using a generator due to the newness of generators?
No. Its just a way to handle OP's problem. you can use generators too.
eg -
...
-
for i in ("%.2f" % float(i) for i in a.split()[1:] ):
-
print i
-
....
-
generators can conserve memory(supposedly from docs) better than list comprehension, especially with large lists.
Thnaks for the reply,
The data I am getting in the dict and the co-ordinates are available in the list.I have to get the co-ordinates formating to 2 decimals and print to the file
I/P : {20200107:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999]}
Regard
PSB
Thnaks for the reply,
The data I am getting in the dict and the co-ordinates are available in the list.I have to get the co-ordinates formating to 2 decimals and print to the file
I/P : {20200107:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999]}
Regard
PSB
Oh... Now you tell us: -
>>> help(dict.get)
-
Help on method_descriptor:
-
-
get(...)
-
D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.
-
-
>>> d = {20200107:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999]}
-
>>> key = 20200107
-
>>> l = d.get(key, (0, 0, 0))
-
>>> s = "%d %.2f %.2f %.2f" %(key, l[0], l[1], l[2])
-
>>> s
-
'20200107 1180.35 223.57 191.21'
-
>>>
bvdet 2,851
Expert Mod 2GB
Thnaks for the reply,
The data I am getting in the dict and the co-ordinates are available in the list.I have to get the co-ordinates formating to 2 decimals and print to the file
I/P : {20200107:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999]}
Regard
PSB
- >>> d = {20200107:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999]}
-
>>> print ' '.join([str(d.items()[0][0]), ] + [s for s in ['%0.2f' % float(i) for i in d.items()[0][1]]])
-
20200107 1180.35 223.57 191.21
-
>>>
If you know the key as in: - for key in d:
-
print ' '.join([str(key), ] + [s for s in ['%0.2f' % float(i) for i in d[key]]])
-
If you are writing multiple items from your dictionary to your file, be sure to add a newline character '\n' at the end of each line.
Oh... Now you tell us: -
>>> help(dict.get)
-
Help on method_descriptor:
-
-
get(...)
-
D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.
-
-
>>> d = {20200107:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999]}
-
>>> key = 20200107
-
>>> l = d.get(key, (0, 0, 0))
-
>>> s = "%d %.2f %.2f %.2f" %(key, l[0], l[1], l[2])
-
>>> s
-
'20200107 1180.35 223.57 191.21'
-
>>>
and for a real world application with more that one entry in the dictionary: -
>>> d = {20200107:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999],
-
... 20200108:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999],
-
... 20200109:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999]}
-
...
-
>>> for k, v in d.items():
-
... print "%d %.2f %.2f %.2f" %(k, v[0], v[1], v[2])
-
...
-
20200107 1180.35 223.57 191.21
-
20200108 1180.35 223.57 191.21
-
20200109 1180.35 223.57 191.21
-
>>>
- >>> d = {20200107:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999]}
-
>>> print ' '.join([str(d.items()[0][0]), ] + [s for s in ['%0.2f' % float(i) for i in d.items()[0][1]]])
-
20200107 1180.35 223.57 191.21
-
>>>
If you know the key as in: - for key in d:
-
print ' '.join([str(key), ] + [s for s in ['%0.2f' % float(i) for i in d[key]]])
-
If you are writing multiple items from your dictionary to your file, be sure to add a newline character '\n' at the end of each line.
Dang, that's good! And fast; you beat me by one minute!
bvdet 2,851
Expert Mod 2GB
Dang, that's good! And fast; you beat me by one minute!
Fastest is not necessarily the bestest! There was something in there that was redundant. Maybe this is it: - d = {20200107:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999]}
-
-
print ' '.join([str(d.items()[0][0]), ] + ['%0.2f' % float(i) for i in d.items()[0][1]])
-
-
# If you know the key, as in:
-
for key in d:
-
print ' '.join([str(key), ] + ['%0.2f' % float(i) for i in d[key]])
>>> 20200107 1180.35 223.57 191.21
20200107 1180.35 223.57 191.21
>>>
just another variation -
>>> d = {20200107:[ 1180.3489999999999, 223.56720000000001, 191.21019999999999]}
-
>>> for i,j in d.iteritems():
-
... print "%s %s" % ( i , ' '.join(["%.2f" % float(f) for f in j]) )
-
...
-
20200107 1180.35 223.57 191.21
-
-
Thanks to all.
It was really helpful for validating my geometry data.
-PSB
Thanks to all.
It was really helpful for validating my geometry data.
-PSB
Which version did you end up going with?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Charles Kerekes |
last post by:
Hello everyone,
I am still learning C++, so I'm not sure what I'm trying to do is
possible. Her is a simplified example:
char Test = 'L';
cout << "Test Char as decimal: " << dec << Test <<...
|
by: Andrew |
last post by:
I trying to format my output to display a set number of decimal
places. I have been trying to use the <iomanip> setprecision(), but
that will only display the total number of digits. Can someone...
|
by: Guoqi Zheng |
last post by:
Dear Sir,
I am trying to get one decimal value from DB. However, every time, I got an
INT back instead of decimal. For example, if I have 4.75 in DB, I will get a
5 back.
The followings are...
|
by: ven |
last post by:
hello i`m makin a asp.net service with database connection where i have an
insert with decimal value, when i run my function i get this error :
There are fewer columns in the INSERT statement...
|
by: spebola |
last post by:
I am using vb.net 2003 professional and I get the following results
when using the round method:
dim Amount as decimal = 180.255
Amount = Amount.Round(Amount, 2)
Amount now contains 180.25. ...
|
by: Microsoft News Group |
last post by:
I am trying to make a column which is decimal format, convert nulls to a
certain string such as "No Max".
This is for a price range, and if they do not put a value in, a null value
is put in...
|
by: Sukanta Bhattacharjee |
last post by:
Hi,
I am in big trouble for the last 10 days and not got any solution in the web.
My problem is : I have a project in .NET 1.1 version, which I have transfered to .NET 2.0 version , almost all are...
|
by: irkahs |
last post by:
Hello all,
I ask this question because I searched and did not find anything that related to this query. My apologies if this is a mistake.
Please see the following code. I am using...
|
by: OuTCasT |
last post by:
I have been trying to save a decimal value into my sql database, what do you use , money, decimal, smallmoney, int ???
|
by: pankajprakash |
last post by:
Hi, I have a decimal number. I need to convert this decimal value to exponential value. I have a vb.net code to convert from decimal to exponential value is Format(100000, #.0#E-##) . When I run this...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |