Home > database >  Splitting String in a column Cell
Splitting String in a column Cell

Time:01-07

Using Python, Is there any way to split strings separated by a comma (5 each at a time) and move them to the next row in the same column? If the number of strings was more than 5. Please see Pic.

Original Data

Desired format

Please find the link to the excel file.

https://docs.google.com/spreadsheets/d/1yJWcDjju-eyDbkNBPUFEkmVXQowuZKX0/edit?usp=sharing&ouid=111158998082042170123&rtpof=true&sd=true

Please Help. Thank you.

I tried some for loops to add empty rows, then move strings. But it didn't work. Please Help. Thanks

CodePudding user response:

You can use a for loop to split the strings and move to the next row. Something like:

   # Define the string to be split 
    string = 'string1, string2, string3, string4, string5, string6, string7, string8' 
   # Split the string into different parts 
    parts = string.split(', ') 
   # Create an empty list to store the split strings 
    split_strings = [] 
   # Iterate over the parts and add them to the list, 5 at a time for i in range(0, len(parts), 5): 
    split_strings.append(parts[i:i 5]) 
   # Print the result: 
    print(split_strings) 
   # Output should be: 
    [['string1', 'string2', 'string3', 'string4', 'string5'], ['string6', 'string7', 'string8']]
  • Related