1

Good day Everybody, I have question, is there away to split the data into fixed length ?

suppose

data=2220591972004161833372381965973430564161832220599125418620936367891254212825967504230783108294828632042934883049336591444611742626636047927395221895991254390547243063380503905471**64**

I want to split into 3 part like:

d1=222059197200416183337238196597343056416183222059912541862093636789125421282596750423078310829482863204293488304933659144461174262663604792739522189599125439054724306338

d3=050390 >>>> length is 6

d4=5471 >>>>> length is 4

Where the last 2 digits of data is 64 are used to determine the length of d3 and d4.

I have seen many example of split function as list and so on but none of these is what i look for such like here and here there is one program written in c++ i want same like in python here

another example for string data split data into to parts same way, last one digit is a size of x2

data2=cc32b326560de95d0fba47b5ad9072418f15caca4c39c2fe4db7003f4b8f81a79
x=cc32b326560de95d0fba47b5ad9072418f15caca4c39c2fe4db7003
x1=f4b8f81a7 >>>>length is 9 

i'm using python 3.6 spyder. thanks all

2
  • I don't quite understand what you're asking here. You say you want to split strings by specific lengths, but you have given two sets of strings with different lengths as examples Commented Jan 30, 2020 at 11:18
  • yes, i want to split the string by specific length , i have mention two example data and data1 both different, the first example data need to divide into 3 parts as i describe into d1 and d3 and d4 , the length of d3 6 and the length of d4 is 4, its already mention last 2 digit Commented Jan 30, 2020 at 11:22

3 Answers 3

1

Assuming data is a string, you could use the following function, which takes data and an integer n, which tells how many characters from the end of the string to use.

def split(data, n):
    indices = list(int(x) for x in data[-n:])
    data = data[:-n]
    rv = []
    for i in indices[::-1]:
        rv.append(data[-i:])
        data=data[:-i]
    rv.append(data)
    return rv[::-1]

Usage:

>>> split(data, 2)
['222059197200416183337238196597343056416183222059912541862093636789125421282596750423078310829482863204293488304933659144461174262663604792739522189599125439054724306338',
 '050390',
 '5471']
>>> split(data2, 1)
['cc32b326560de95d0fba47b5ad9072418f15caca4c39c2fe4db7003', 'f4b8f81a7']

If you want n to always be 2, just edit the function as follows:

def split(data):
    indices = list(int(x) for x in data[-2:])
    data = data[:-2]
    rv = []
    for i in indices[::-1]:
        rv.append(data[-i:])
        data=data[:-i]
    rv.append(data)
    return rv[::-1]

To assign to variables, then, you could use

>>> d1, d2, d3 = split(data)
>>> d1
'222059197200416183337238196597343056416183222059912541862093636789125421282596750423078310829482863204293488304933659144461174262663604792739522189599125439054724306338'
>>> d2
'050390'
>>> d3
'5471'
Sign up to request clarification or add additional context in comments.

3 Comments

not working and give TypeError: 'long' object has no attribute 'getitem' , but still i need to divide into 3 part, the value of n read it from the data it self , its last 2 digit is the length
@honey as I said, this assumes that data is a string. Try with split(str(data), 2)
@honey How do you decide the value of n? You have two examples in your post, one splits into two parts, one splits into three parts.
1
data=222059197200416183337238196597343056416183222059912541862093636789125421282596750423078310829482863204293488304933659144461174262663604792739522189599125439054724306338050390547164

data = str(data)
length_d3 = int(data[-1])
length_d2 = int(data[-2])
data = data[0:-2]

d1 = int(data[0:-(length_d3+length_d2)])
d2 = int(data[-(length_d3+length_d2):-length_d3])
d3 = int(data[-length_d3:])

print(d1)
print(d2)
print(d3)

Comments

0

For the specific lengths you have provided, this will split them properly

data="222059197200416183337238196597343056416183222059912541862093636789125421282596750423078310829482863204293488304933659144461174262663604792739522189599125439054724306338050390547164"
d1 = data[:-12] # From the start to 12 characters from the end
d3 = data[-12:-6] # From 12 characters from the end to 6 characters from the end
d4 = data[-6:-2] # From 6 characters from the end to 2 characters from the end

3 Comments

how to make the specific length is dynamic not static, because the data will be have different size, i want the size length take it from last 2 digit of original data
If what you want is to (ignoring the last two characters) have d4 be the last 4 characters, d3 be the last 6 characters before that, and d1 to be the rest, then this will work for all lengths of data (as long as they’re at least 12 long)
ya sir, the point of last 2 digits is to specify the length of d3 and d4, because the d1 is dynamic and the length will change so it cant be 12 all time , thank you sir, your code is work as you said with fixed size of d1, but if we need to make it as dynamic size? where the last 2 digit from the string take it automatically to define the size of d3 and d4 and the remaining is d1. what change we can do?

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.