python


Formatting string to certain character limit in Python


I have a code that outputs a whole bunch of numbers after doing some maths on them. At one point in the code they are rounded off with numpy.rint, and in certain cases (I believe when a 9 is rounded to a 10) I end up with a trailing zero that I do not want. I have some code that looks sort-of like this
ra3n = ra3/60 * 10
ra3n = np.rint(ra3n)
ra3n = ra3n.astype(str) ##there is a good reason that this needs to be a string
I need all of the resulting ra3n to be 5 characters long, but occasionally one pops out as 6 characters long. How would I format this properly? Keep in mind I'm a total python noob, so I might need it spelled out for me =)
EDIT:
Here's my output:
00244-2451
00244-2702
00278-0629
00286-1614
00295-1101
002910-0546
00303+0711
00305+2246
00348+2604
003410+0423
00355-0204
00359+1236
00360-0931
00386-1210
The instances where there are six digits instead of 5 in the first half of the string are the erroneous ones; those trailing zeroes should not be there.
ra3n = ra3n[:-1] if ra3n[-1] == '0' else ra3n
There's probably a better solution, but I'm not sure I really understand your issue without seeing some output.
You change the type of ra3n, which is poor programming practice. Try this.
ra3n = format(ra3/60.*10., '5f')[:5]
This gives exactly five characters. Note that if the string would usually be six characters long, this cuts off the last character, for good or for bad. Note also that I included decimal points in the 60 and 10 numbers: this guarantees that floating-point division will be used, rather than integer division if this is done in Python 2.

Related Links

(Beginner)Python functions Codeacademy
Python script hangs
How to shift a list nth time in Left or Right and fill it up with 0 in python
NumPy, why equality check does not work for an array of objects?
String formatting of timedeltas in Pandas
Celery - How to send task from remote machine?
YouTube Api v3 handling Exceptions in Python
Copy data from one oracle database to another with Python
converting python program into executable
How to use multidatabase across multiproject in django?
SSLHandshakeError when connecting to Google Analytics using Google APIs Client Library for Python
Pandas Pivot Table Display in ReportLab
python multiprocessing behaviour
How do You Hack the Enthought Canopy Editor Background Color?
Why does PySide still require QString for textChanged SIGNAL?
Calling to a Sikuli script from Python (Selenium)

Categories

HOME
openssl
project-management
drupal-7
bing
actionscript
h2
automated-tests
wampserver
specflow
apple-numbers
hyperledger-fabric
rebol
xtext
yocto
cortex-a
gatsby
spinnaker
uisplitviewcontroller
medium-editor
yeoman-generator
quartus
derived
ng-show
ejbca
clover
distance
plsqldeveloper
deb
sensu
cocoa-touch
header-files
myob
csh
samsung-mobile
printdocument
croppic
helper
dd-wrt
referenceerror
svnkit
polymorphism
shapes
reactivemongo
windows-10-iot-core
sidr
broadleaf-commerce
rhel.net
elfinder
ntfs-mft
equivalence
restful-url
intellij-idea-2016
pdftk
stress-testing
apiary
double-buffering
proof
sgmlreader
livescribe
tiddlywiki
slidesjs
fontconfig
vici
createobject
pspice
cleditor
c#-interactive
chessboard.js
git-annex
spoofing
achievements
gradle-release-plugin
aerogear
fill
yoothemes
portfolio
except
affix
selendroid
textkit
abas
urn
ytplayerview
viper-architecture
pgm
ember-cli-addons
sysfs
libsndfile
fragment-tab-host
jstat
suffix-array
outline
io.js
planetary.js
pic24
kraken.js
voldemort
bounds
initialization-vector
real-time-updates
convex-polygon
modeshape
driver-signing
jqmobi
nuspec
arbor.js
maven-ear-plugin
subtract
windows-phone-7-emulator
responsetext
makefile-project
point-sprites
serp
ruby-debug
symbol-server
pureftpd
loadui
chdatastructures
visual-c++-2008-express
database-diagramming

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App