-1

I want to split the data instance from the line as shown in image below.

start_end_time, wav_file_name, emotion, val_act_dom = line.strip().split('\t')

I need to store first column time ranges in start_end_time, txt file name in wav_file_name, third column of data in emotion and fourth column list in val_act_dom. This code is throwing me error saying "expected 4 but got 1". Is there any other easier way to achieve that? Also, I have already used loop for all lines and I couldn't do it for even single line.

Sorry but my line looks like this as shown in new image: enter image description here

P.coder
  • 11
  • 2
  • Do you have your data in a list? – Alex Metsai Feb 25 '21 at 08:16
  • Please do not post pictures of code. This is hard to copy and past. – mosc9575 Feb 25 '21 at 08:17
  • It looks like each line is a list with four elements. So `start_end_time, wav_file_name, emotion, val_act_dom = line` could work. – mosc9575 Feb 25 '21 at 08:18
  • 1
    Are you reading the lines from a text file (i.e. line is a str) or are they already python lists? What are you trying to achieve with strip and split? By the look of the data, it's comma separated and splitting on \t would look for tabs. The error indicates that you are trying to unpack an iterable to your 4 variables but the iterable only contains 1 value (which makes sens if line is str since there are no tabs -> split("\t") would return a list with 1 item). – tbjorch Feb 25 '21 at 08:21
  • @tbjorch my data looks like in new edit I have done. Please check. – P.coder Feb 25 '21 at 08:29
  • Are your data in a text file then or not? What is the type of line? – Alex Metsai Feb 25 '21 at 08:29
  • Yes it is in text file and I have converted to the format as shown in image. Just want to store them separately as in piece of code I have mentioned. @AlexMetsai – P.coder Feb 25 '21 at 08:31
  • 1
    The input you're working on does not always have tabs separating the substrings. You can use regex like in the last edit of my answer to track multiple whitespaces. Let me know if it works – BiOS Feb 25 '21 at 09:03

1 Answers1

0

I could reproduce the problem with the following code:

line = "[192.5200 - 200.3000]    Ses04F_script02_2_F02_F020 sad [4000,     35000, 4000]"
start_end_time, wav_file_name, emotion, val_act_dom = line.strip().split('\t')

That will throw:

ValueError: not enough values to unpack (expected 4, got 1)

The reason for which it is happening is the formatting of your input. If you take a look, spacing between values in your string are not uniform. Only the wav_file_name and the start_end_time are separated by a tab, the rest are separated by normal spaces. Make sure to set uniform and unequivocal spacing between your values.

The below works for me:

line = "[192.5200 - 200.3000]    Ses04F_script02_2_F02_F020    sad    [4000, 35000, 4000]"

start_end_time, wav_file_name, emotion, val_act_dom = line.strip().split('    ')

Especially if you cannot modify your source, an alternative and possibly better solution would be to use regex and split the line by multiple occurrences of whitespace:

import re

line = "[192.5200 - 200.3000]    Ses04F_script02_2_F02_F020    sad    [4000, 35000, 4000]"

start_end_time, wav_file_name, emotion, val_act_dom = re.split(' {2,}', line.strip())
BiOS
  • 2,282
  • 3
  • 10
  • 24