[Tutor] Concatenating numeric data in Python 3.3

sparkle Plenty sparkle.plenty12481632 at gmail.com
Wed Apr 24 20:23:26 CEST 2013


Thanks to both of you for your assistance.
Since the completed message must be in hex, I also have an issue with
losing high order zeros during conversions, although the binary shift works
well for adding on to the end of the string.  I cannot pack string data,
and I cannot concatenate numbers, so the conversions, although smelly, were
what I knew to try.  I welcome the idea of abandoning that course of
action.  It didn't work anyway.

 I will have an unknown number of instances, each of which consists of 3
numeric values.  I tried packing each instance using struct.pack, then
converting to  a tuple, then concatenating.  This ran, but introduced
errors: in some instances, high order truncation of a leading zero in the
first of the 3 values.  This throws off the position of all data that
follows.  Error messages and error code are dependent on which technique is
being tried.  The most recent ones are:
TypeError: can't concat bytes to str
TypeError: Can't convert 'tuple' object to str implicitly




On Wed, Apr 24, 2013 at 1:30 PM, Prasad, Ramit <ramit.prasad at jpmorgan.com>wrote:

> 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.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130424/5464ff51/attachment.html>


More information about the Tutor mailing list