I'm curious if their is some python magic I may not know to accomplish a bit of frivolity
given the line:
csvData.append(','.join([line.split(":").strip() for x in L]))
I'm attempting to split a line on :
, trim whitespace around it, and join on ,
problem is, since the array is returned from line.split(":")
, the
for x in L #<== L doesn't exist!
causes issues since I have no name for the array returned by line.split(":")
So I'm curious if there is a sexy piece of syntax I could use to accomplish this in one shot?
Cheers!
the split() method in Python split a string into a list of strings after breaking the given string by the specified separator. Python String join() method is a string method and returns a string in which the elements of the sequence have been joined by the str separator.
Use split() method to split by delimiter. If the argument is omitted, it will be split by whitespace, such as spaces, newlines \n , and tabs \t . Consecutive whitespace is processed together. A list of the words is returned.
what does input(). strip(). split(' ') in python means?? input() - takes input from user strip() - deletes white spaces from the begin and the end of input split(' ') - splits input into elements of an list with (' ') being as separator.
>>> line = 'a: b :c:d:e :f:gh '
>>> ','.join(x.strip() for x in line.split(':'))
'a,b,c,d,e,f,gh'
You can also do this:
>>> line.replace(':',',').replace(' ','')
'a,b,c,d,e,f,gh'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With