简体   繁体   中英

Remove leading and trailing white spaces in a csv file

Define:

str = " a , b,c, hello there ! , my name is +++ , g "

How do I remove the leading and trailing white spaces so that I output:

"a,b,c,hello there !,my name is +++,g"

ie the output is such that there is no leading or trailing white space between values in a comma separated value string.

I started reading about Regular Expressions? Would this be an appropriate situation to use that? How would I go about completing the task?

You can usesplit() ,strip() and join() like this:

','.join([item.strip() for item in my_string.split(',')])

Output:

>>> my_string = " a  , b,c, hello there !   ,   my name is +++ ,  g "
>>> ','.join([item.strip() for item in my_string.split(',')])
'a,b,c,hello there !,my name is +++,g'

Explanation:

split() is used to split my_string by the separator , and the result is the following list:

>>> my_string.split(',')
[' a  ', ' b', 'c', ' hello there !   ', '   my name is +++ ', '  g ']

strip() is used to remove leading and trailing spaces from each item of the previous list:

>>> [item.strip() for item in my_string.split(',')]
['a', 'b', 'c', 'hello there !', 'my name is +++', 'g']

The above line is called a list comprehension

join() is used to form the last result by joining the items of the above list.

You can use the strip functionality

string = '     blah blah   '
print string.strip() # =>'blah blah'

You may use regex expression '\\s*,\\s*' to replace the matching content with , . For example:

>>> import re
>>> my_str = "[ a  , b,c, hello there !   ,   my name is +++ ,  g ]"

>>> re.sub('\s*,\s*', ',', my_str)
'[ a,b,c,hello there !,my name is +++,g ]'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM