Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell Pandas read_csv to use multiple whitespaces as separators but not single whitespaces?

I want to read in a Pandas dataframe from csv, where there are single whitespaces inside column names and the separators are multiple whitespaces. How can I tell Pandas to use only more than one consecutive whitespace as separator but ignore single whitespaces?

like image 916
Julia K Avatar asked Oct 28 '19 16:10

Julia K


1 Answers

With specific regex pattern for sep= option:

df = pd.read_csv(sep='\s{2,}')
  • \s{2,} - quantifier, matches any whitespace character between 2 and unlimited times, as many times as possible
like image 108
RomanPerekhrest Avatar answered Oct 02 '22 21:10

RomanPerekhrest