2

I have a string and I'm trying to iterate it in blocks and save the value of each block into a list, but, without success.

string_A = '920dfffffffff27aff024932090901212003024937090901212003024942090901212003024947090901212003'
newFileTtsInHexString = []
finalListFile = []
for element in string_A[0:len(string_A):18]:  
        newFileTtsInHexString = element
        print("\n=== newFileTtsInHexString ==="+newFileTtsInHexString)
        finalListFile = '\n'.join(newFileTtsInHexString)

that's the output :

=== newFileTtsInHexString ===9
=== newFileTtsInHexString ===0
=== newFileTtsInHexString ===0
=== newFileTtsInHexString ===0
=== newFileTtsInHexString ===0

But my goal is to have the finalListFile like this:

920dfffffffff27aff
024932090901212003
024937090901212003
024942090901212003
024947090901212003

in blocks of 18 characters.

baduker
  • 19,152
  • 9
  • 33
  • 56
gfernandes
  • 193
  • 1
  • 10
  • Main problem is how you take the slice and for loop iteration through each element iterates character by character. ```for element in string_A[0:len(string_A):18]```. Slice [0:len(string_A):18] is "90000", for element in '90000' iterates through '9' '0' '0' '0' '0'. Slice [0:18] is the first chunk you want "920dfffffffff27aff". – gaoithe Jan 26 '21 at 15:29
  • Get rid of for loop, something like this instead of the for would work: ```while string_A: element = string_A[0:18] string_A = string_A[18:] ``` – gaoithe Jan 26 '21 at 15:31
  • Not really a duplicate question, ... ~ sort of ~ ... as the unintended question(s) here are really on why doesn't ```for element in string_A[incorrectSlice]``` work? Issues are: 1. fixing slice, 2. iterating char by char, 3. program structure for iterating through the string in chunks. – gaoithe Jan 26 '21 at 15:35
  • How does slicing work? slice[start:stop] returns the chunk from start to stop. The manual is a bit, well, manuall-y https://docs.python.org/2.5/ref/slicings.html Interestingly the third stride argument was a later addition slice[start:stop:stride] https://docs.python.org/2.3/whatsnew/section-slices.html. I would say the stride is not used much. – gaoithe Jan 26 '21 at 15:46

2 Answers2

4

Is this wnat you want?

s = '920dfffffffff27aff024932090901212003024937090901212003024942090901212003024947090901212003'

print([s[i:i+18] for i in range(0, len(s), 18)])

Output:

['920dfffffffff27aff', '024932090901212003', '024937090901212003', '024942090901212003', '024947090901212003']
baduker
  • 19,152
  • 9
  • 33
  • 56
2

If I'm not completely wrong, you need to do this differently:

string_A = '920dfffffffff27aff024932090901212003024937090901212003024942090901212003024947090901212003'
newFileTtsInHexString = []
finalListFile = []
for i in range(len(str)//18): # iterates through your string in blocks
    string = string_A # so we don't crush our string
    newFileTtsInHexString[i] = string[i*18:(i+1)*18] # get the substring and append to our list
    finalListFile = '\n'.join(newFileTtsInHexString)

Not tested, could contain errors.

Fabian
  • 96
  • 5