python的分隔符,Python-如何使用多个分隔符拆分列值

I am reading a .csv file and creating a Panda Dataframe. From this Dataframe I am fetching a value which is supposed to be a "list" item with comma separated values in it. But it comes out as a "string" item and I have to use a separator to split the values in it.

For example : I have a string variable by name "column_names" with below values

column_names = "First_Name, Last_Name,Middle_Name"

column_names = column_name.split(',')

Please note the space before the second value. So when I print this variable, I would be getting a space before the second element which will further create trouble while extracting values from this variable.

print(column_names)

['First_Name', ' Last_Name', 'Middle_Name']

In order to overcome this, if I keep separator to have a space along with actual separator (here it will be ', ' ), then the values are not getting splitted properly as seen below

column_names = "First_Name, Last_Name,Middle_Name"

column_names = column_names.split(', ')

print(column_names)

['First_Name', 'Last_Name,Middle_Name']

Notice the space to the right of comma while splitting. Using this separator, I am able to get only two values instead of three values.

My problem is the variable may contain comma separated values along with a space to the left or right of the comma or there may be no space at all. I have to handle all the cases with a single command (if possible). Something like providing multiple separator values while splitting.

For example : column_names.split(','|', '|' ,').

Not sure whether there is any as such but any pointers to this will be helpful.

解决方案

This is a common issue with CSVs. Fortunately, you can nip this in the bud, simply by reading your CSV properly, so you don't have to do all this unnecessary post-processing later.

When reading your dataframe with read_csv, pass a regex to sep\ delimiter -

df = pd.read_csv(..., sep='\s*,\s*', engine='python')

Now, df.columns should be a list of strings.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值