Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using an iSeries Access ODBC Driver to try and run queries against DB2 for i. I'd like to make this information coming back more readable. How can I convert the byte array information using python? Example output below:

>>> cursor.execute("SELECT * FROM QAAPFILE$") 
<pyodbc.Cursor object at 0x00C6D2C0>
>>> for row in cursor:
...     print row         


Example Output:
(1, bytearray(b'\xfd@@@@@@@@@'), 1, 1, 8, 9, bytearray(b'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'), bytearray(b'@@@@@@@@@@@@@@@@@@@@'), bytearray(b'\x00<\x02\x82B\x02\x02<\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'))
Many more rows
share|improve this question
 
You should elaborate on what you want by 'readable'. Obviously it's going to depend on the content of the bytearrays and how they should be interpreted. –  brak2718 yesterday
 
I am hoping to convert the retrieved byte array information into alphanumeric characters (English + Numbers). I have no idea what is stored in these tables and need to figure out what everything is. –  user1645914 yesterday
1  
The file QAAPFILE$ contains the symbol set for small symbol definitions, it's related to PFD definitions. Your inventory information should not be stored in QGPL (which is where the QAAPFILE$ table is located. –  Benny Hill yesterday
1  
By convention, any object name that starts with a 'Q' is an IBM created object. This is an obscure system file, for an old print utility, and will tell you nothing about inventory. Get some guidance from your IT department. –  WarrenT yesterday
1  
When you do find out what tables (files) in what schemas (libraries) you should be looking at, try using IBM Data Studio, IBM i Access for Windows Navigator, or something like SQL Squirrel to look at the database. And use the IBM i Information Center SQL reference to give you just the data you need. Connect with "system naming" in your connection string, to enable use of the "library list" search path, so that you don't hard-code schema names in your Python or SQL code. –  WarrenT yesterday
show 3 more comments

1 Answer

I'm not sure why you're looking at QAAPFILE$, and I'm not sure the data retrieved from it must be retrieved as bytearray. But if you absolutely must read raw bytes, then the first thing you need to know is that the IBM i (AS/400, iSeries, etc.) is natively EBCDIC. The '@' is a telltale sign that you need to use decode('cp037'). In code page 37 (standard American EBCDIC, you may need to adjust slightly for different locales), a space is '\x40', which happens to be the same as '@' in ASCII or UTF-8.

Now, I will say that the samples you've given don't decode nicely, so there's unfortunately more to the story. Like the commenters, I'll need more information. (For example, still really mystified why you'd need to look at QAAPFILE$. And, for the record, I have never seen or used that file. I have used pyODBC plenty of times to connect to the i, and I've never had to use bytearray.)

Edit: I've now had a chance to look at QAAPFILE$ on the machine where I work, and I can tell you that you're not going to find readable alphanumeric characters in it. Well, that second column (the first bytearray element in the row tuple) is actually readable with the proper decoding, but the last one really isn't, not even looking at it directly on the server using a native interface. It really is just binary data. I can't imagine what use you could have for this file.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.