Python Forum
Having a hard time conceptualizing how to print something
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Having a hard time conceptualizing how to print something
#1
I have a table of bytes (actually they're bit flags, but one concept at a time), as below.
FB 2C D2 93 40 00 00 00 00 00 00 00 EA C8 61 00 
F9 AA F0 D7 C0 00 00 00 00 00 00 00 FD 28 CA 00 
E2 49 B4 8B 50 00 00 00 00 00 00 00 F2 4B CE 00 
E8 99 A4 4B B0 00 00 00 00 00 00 00 E1 37 D7 00 
82 0E 00 C3 40 00 00 00 00 00 00 00 DE 89 19 00 
F5 F1 FB 1F F0 00 00 00 00 00 00 00 E3 FE E6 00 
FA E7 D2 DF D0 00 00 00 00 00 00 00 F5 EC F5 00 
FF F9 FF 1F F0 00 00 00 00 00 00 00 F4 BD 79 00 
FB C6 92 D3 20 00 00 00 00 00 00 00 FF FA FE 00 
FB C9 54 03 D0 00 00 00 00 00 00 00 FF 13 0A 00 
C9 96 92 CB B0 00 00 00 00 00 00 00 C5 1E BD 00 
FB EE D2 C3 77 00 00 00 00 00 07 FC EA E9 F9 00 
FD F9 FD 57 E0 00 00 00 00 00 00 00 F7 7D 7D 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
I want to print it with the columns and rows switched, as done partially below.
FB F9 ...
2C AA ...
D2 F0 ...
93 D7 ...
40 C0 ...
00 00 ...
00 00 ...
00 00 ...
00 00 ...
00 00 ...
00 00 ...
00 00 ...
EA FD ...
C8 28 ...
61 CA ...
00 00 ...
I'm having a hard time mentally grasping how to track and iterate the seeks to do this.

My code:
import io,sys

with open(sys.argv[1], 'rb') as scus:
    baseAddr =58364 
    #scus.seek(baseAddr)
    #print(scus.read(1)[0], end='\' ')
    with open("sparks.txt", 'w') as writer:
       listCtr = 0
       entryCtr = 0
       while listCtr < 16:
           scus.seek(baseAddr + listCtr + entryCtr)
           print(hex(scus.read(1)[0])[2:].zfill(2) + "-")
           listCtr += 1
       print()
My output:
python extractSparkTalents.py SCUS_942.30
fb-
2c-
d2-
93-
40-
00-
00-
00-
00-
00-
00-
00-
ea-
c8-
61-
00-
It seems like it requires recursion, or maybe a specialized type of string splitting where you put line breaks between the strings.

I'm not sure if you can seek in lines in a text file either, and how writing behaves in such a case.

edit

Or maybe it requires stores, since it's sort of a sorting algorithm?
Reply
#2
I'm not clear how you're printing the first table. Do you just have bytes and you're choosing to print it in 16 columns, or is it text?

What I would do is create a container with 16 lists inside. Then as you read each "line" of the source, append the correct column to the correct list.

When you're done, print out each list in order.
Reply
#3
(Sep-19-2020, 07:36 PM)bowlofred Wrote: I'm not clear how you're printing the first table. Do you just have bytes and you're choosing to print it in 16 columns, or is it text?
It is a 16 x 16 table of bytes in a file, which I have copied as a hex string from a hex editor and manually added line breaks.

It's a table of data from a video game, for which I'm writing a simple editor. The format requires I switch rows and columns, and I need a script for doing this so people can extract and view changes they or others previously made.

(Sep-19-2020, 07:36 PM)bowlofred Wrote: What I would do is create a container with 16 lists inside. Then as you read each "line" of the source, append the correct column to the correct list.

When you're done, print out each list in order.
I suppose that is the simplest concept, which makes it the best solution for me.
Reply
#4
If the data were in a matrix, isn't it just a transpose? Bumpy could be your friend.
Reply
#5
(Sep-19-2020, 08:03 PM)ndc85430 Wrote: If the data were in a matrix, isn't it just a transpose? Bumpy could be your friend.
Not an option because:
1. I can't force people to install an entire library just to use a single function in it.
2. I don't want to spend 10x on much time figuring out licensing BS as I do on coding.

This is intended to be a simple script.
Reply
#6
In line 12, inside the right parenthesis add
end = ''
Reply
#7
(Sep-19-2020, 08:58 PM)jefsummers Wrote: In line 12, inside the right parenthesis add
end = ''
Useless in this context as I'm trying to transpose a matrix, apparently. If I were just trying to print one row as a column your solution would be perfect, but I'm not.

How do I create a list of lists? This doesn't work.
import io,sys

with open(sys.argv[1], 'rb') as scus:
    baseAddr =58364 
    with open("sparks.txt", 'w') as writer:
        scus.seek(baseAddr)
        tblList1 = []
        cntr = 0
        while cntr < 16:
            tblList1.append(scus.read(16))
            cntr += 1
edit

I wrote code whose output must be seen to be believed.

Code 1:
import io,sys

with open(sys.argv[1], 'rb') as scus:
    baseAddr =58364 
    scus.seek(baseAddr)
    with open("sparks.txt", 'w') as writer:
        tblList1 = []
        cntr = 0
        while cntr < 16:
            tblList1.append(scus.read(16))
            #tblList1.insert(cntr, scus.read(16))
            cntr += 1
            for i in tblList1:
                for j in i:
                    print(hex(j)[2:].zfill(2) + "-", end='')
                print()
Output 1:
https://pastebin.com/9rr4XDsD

Code 2:
import io,sys

with open(sys.argv[1], 'rb') as scus:
    baseAddr =58364 
    scus.seek(baseAddr)
    with open("sparks.txt", 'w') as writer:
        tblList1 = []
        cntr = 0
        while cntr < 16:
            #tblList1.append(scus.read(16))
            tblList1.insert(cntr, scus.read(16))
            cntr += 1
            for i in tblList1:
                for j in i:
                    print(hex(j)[2:].zfill(2) + "-", end='')
                print()
Output 2:
https://pastebin.com/e6Aq26KB

I don't know if it's a "feature" of lists in Python, or something to do with the read() method used in binary mode, but it's apparently adding redundant lines over and over again. What should be a 16x16 list becomes a 138x16 list.

edit

Disregard, I am a moron. Here's the fixed code:
import io,sys

with open(sys.argv[1], 'rb') as scus:
    baseAddr =58364 
    scus.seek(baseAddr)
    with open("sparks.txt", 'w') as writer:
        tblList1 = []
        cntr = 0
        while cntr < 16:
            #tblList1.append(scus.read(16))
            tblList1.insert(cntr, scus.read(16))
            cntr += 1
        for i in tblList1:
            for j in i:
                print(hex(j)[2:].zfill(2) + "-", end='')
            print()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Hard time trying to figure out the difference between two strings carecavoador 2 646 Aug-16-2023, 04:53 PM
Last Post: carecavoador
  Print names in x-axis of a time-series values hobbyist 4 1,178 Apr-22-2023, 09:29 PM
Last Post: deanhystad
  How to print results of asyncio websockets at the same time? codingmonster 0 1,744 Jun-04-2021, 01:48 PM
Last Post: codingmonster
  Conceptualizing modulus. How to compare & communicate with values in a Dictionary Kaanyrvhok 7 3,908 Mar-15-2021, 05:43 PM
Last Post: Kaanyrvhok
  Print characters in a single line rather than one at a time hhydration 1 1,991 Oct-10-2020, 10:00 PM
Last Post: bowlofred
  How to print n days back date at give time Mekala 1 1,956 Oct-10-2020, 03:35 AM
Last Post: bowlofred
  Having hard time understanding the function self-returning itself twice jagasrik 2 2,450 Aug-15-2020, 08:50 PM
Last Post: deanhystad
  The count variable is giving me a hard time in this code D4isyy 2 1,929 Aug-09-2020, 10:32 PM
Last Post: bowlofred
  Print a certain string only the first time it appears in a test file buttercup 5 2,699 Jul-23-2020, 01:30 PM
Last Post: palladium
  Having a hard time combining two parts of code. Coozeki 6 3,011 May-10-2020, 06:50 AM
Last Post: Coozeki

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020