-1

I am parsing sets of csv-type data into Python tuples by splitting the strings across a delimiting character. Very simple stuff.

My issue is that any of the fields could potentially contain empty strings as valid data. This is fine, except if the very last field is a empty string, in which case the length of the resulting tuple is one less than it should be.

For instance, given the following string, with commas as delimiters :

"2016-08-03,jim,,5146,,ok,,2,"  

I desire the following output:

["2016-08-03", "jim", "", "5146", "", "ok", "", "2", ""]

While trying to find a simple solution to this problem, I found this answer, which details how to retain trailing empty strings in Java's split implementation. However, I have been unable to find anything equivalent in Python. Are there any alternative standard library methods or other simple tricks that would produce this behavior, or will I need add some additional logic into the script to accomplish this?

(I realize that it would be very simple to write a new method that produces this output, but in the interest of less code is better from a maintenance standpoint, I thought I would first check to see if I am missing something even easier.)

Community
  • 1
  • 1
user3351605
  • 1,271
  • 3
  • 19
  • 30

1 Answers1

0

I think python directly gives the correct output. Do you have any specific example where this is not the case? :

x = "5|6|7||8|9||"

x.split("|")
Out: ['5', '6', '7', '', '8', '9', '', '']
Gaurav Dhama
  • 1,346
  • 8
  • 19
  • You're correct. Turns out that program producing the data was adding an additional control character, effectively masking the last value when it was empty. – user3351605 Aug 03 '16 at 17:57
  • Great. Happy to help!! Please accept as correct if you found the solution to your issue – Gaurav Dhama Aug 03 '16 at 17:59