[Tutor] Concatenating numeric data in Python 3.3
Prasad, Ramit
ramit.prasad at jpmorgan.com
Wed Apr 24 19:30:14 CEST 2013
sparkle Plenty wrote:
> Sent: Wednesday, April 24, 2013 11:52 AM
> To: Tutor at python.org
> Subject: [Tutor] Concatenating numeric data in Python 3.3
>
> What is the best way to concatenate packed numeric data? I am building a message to send to a device
> and it has a very specific header format and variable length payload. Converting to string,
> concatenating, and then converting back to numeric introduced errors. The tuple() function also
> introduced errors.
>
> The code is proprietary so I am not comfortable posting it. I have been programming in Python for a
> few weeks. It is my first OOP language. My background, in the dim and distant past, is mainframe.
>
> Any help would be greatly appreciated. Thank you in advance.
Try reducing the error section to the minimal amount of code (or create example that
has the same problem) and post that.
Converting back and forth between strings and numeric types is often a sign of smelly
code. Why do you need to convert back and forth? Not that conversion is bad, just a
sign of a possible design problem.
Repeated string concatenation can be a slow and wasteful procedure.
Use str's join method to build a string.
>>> ','.join( [ 'a','b','c'] ) # Argument to join must be an iterable (list/set/etc)
'a,b,c' # of strings. Convert data to str before joining.
>>> ' '.join( [ 'a','b','c'] )
'a b c'
~Ramit
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
More information about the Tutor
mailing list