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.

4
  • 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". Commented Jan 26, 2021 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:] Commented Jan 26, 2021 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. Commented Jan 26, 2021 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 docs.python.org/2.5/ref/slicings.html Interestingly the third stride argument was a later addition slice[start:stop:stride] docs.python.org/2.3/whatsnew/section-slices.html. I would say the stride is not used much. Commented Jan 26, 2021 at 15:46

2 Answers 2

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']
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

hey Fabian, thanks, with some minors corrections, the code worked fine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.