1

I'm currently trying to slice a specific string into parts but alway getting out of index errors.

The string is:

columnData = "001.001.000.100.000.000.000"

myClassInstance = MyClass(
    param1 = columnData[0:3],  
    param2  = columnData[4:3], 
    param3 = columnData[8:3],  
    param4  = 0,             
    param5 = columnData[12:3], 
    param6 = columnData[16:3], 
    param7 = columnData[20:3], 
    param8 = columnData[24:3]  

)

Whenever it tries to set param8 I get the out of index error.

I then tried to put the slices into a file to see where the error is and tried:

                        f = open("TestmyTset.txt","w")
                        f.write(columnData)
                        f.write("\nparam1: ")
                        f.write(columnData[0:3])
                        f.write("\nparam2r: ")
                        f.write(columnData[4:3])
                        f.close();

but param2 was never printed into the file.

The output is:

001.001.000.100.000.000.000
param1: 001
param2: 

So my question here is where my error is as I tried again and again and not finding it (in effect each of the slices shall be one of the . separated parts of the string).

As asked the expected outputs would be:

param1 = "001"
param2 = "001"  (the 2nd 001 in the original string)
param3 = "000"
param4 = "0"
param5 = "100"
....
5
  • 2
    What answer do you expect from columnData[4:3]? Commented Nov 22, 2014 at 10:20
  • I updated the answer there so that it is clear what I would expect inside each param from the appropriate slices Commented Nov 22, 2014 at 10:21
  • In param4 you are expecting "100" but you have declared it as 0. Commented Nov 22, 2014 at 10:24
  • tnx was a typing error corrected it Commented Nov 22, 2014 at 10:26
  • ok got it from the answers.....ouch confused languages again (thought length of slice instead of stop location) Commented Nov 22, 2014 at 10:28

5 Answers 5

3

i think what you are trying to do is:

columnData[4:7] columnData[8:11] etc.

I think it's better to just to

split_column_data = columnData.split('.')

which splits the string at each . in the string. and returns a list

>>>print split_column_data

['001', '001', '000', ... ]

Sign up to request clarification or add additional context in comments.

Comments

2

The documentation for slice() states:

slice([start], stop[, step])

Return a slice object representing the set of indices specified by range(start, stop, step). The start and step arguments default to None. Slice objects have read-only data attributes start, stop and step which merely return the argument values (or their default). They have no other explicit functionality; however they are used by Numerical Python and other third party extensions. Slice objects are also generated when extended indexing syntax is used. For example: a[start:stop:step] or a[start:stop, i].

In other words, what you want is:

param2 = columnData[4:7]
param3 = columnData[8:11]
...

Comments

1

as far as I have understood your question, it seems that you want to slice a string such that

columnData = "001.001.000.100.000.000.000"

myClassInstance = MyClass(
    param1 = columnData[0:3],  
    param2  = columnData[4:3], 
    param3 = columnData[8:3],  
    param4  = 0,             
    param5 = columnData[12:3], 
    param6 = columnData[16:3], 
    param7 = columnData[20:3], 
    param8 = columnData[24:3]  
)

must set

param1 : 001
param2 : 001
param3 : 000
param4 : 0
param5 : 100
param6 : 000
param7 : 000
param8 : 000

now as far as I know

a = "123456789" to 123
print a[0:3]
print a[3:6]
.....

will give

123
456
....

so for your example you need to set

columnData = "001.001.000.100.000.000.000"

myClassInstance = MyClass(
    param1 = columnData[0:3],  
    param2  = columnData[4:7], 
    param3 = columnData[8:11],  
    param4  = 0,             
    param5 = columnData[12:15], 
    param6 = columnData[16:19], 
    param7 = columnData[20:23], 
    param8 = columnData[24:27]  
)

It will work :) good luck !

1 Comment

Accepted as it was the most complete answer (also yepp works tnx)
1

Slicing in python goes as: [start_index : end_index : step_size] - in other words what you're trying to do when you access columnData[4:3] is read from index 4 to index 3 - which is meaningless.

What you need to read is columnData[4:7] to get the result you desire.

I would also recommend you look at str.split, since columnData.split('.') will give you what you want in a far easier way.

Comments

1
columnData = "001.001.000.100.000.000.000"
param1,param2,param3,param5,param6,param7,param8 = columnData.split('.')
param4 = 0
print ("param1 :{} , param2 :{}, param3 :{}, param4: {}, param5 :{}, param6 :{}, param7 :{},param8 :{}").\
            format(param1, param2, param3, param4, param5, param6, param7, param8)


Output:
param1 :001 , param2 :001, param3 :000, param4: 0, param5 :100, param6 :000, param7 :000,param8 :000

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.