Julia K
Julia K

Reputation: 407

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?

Upvotes: 5

Views: 1607

Answers (2)

Quang Hoang
Quang Hoang

Reputation: 150825

another option that I actually use, which saves me some shift keypresses:

df = pd.read_csv('file.csv', sep='\s\s+')

Upvotes: 3

RomanPerekhrest
RomanPerekhrest

Reputation: 92904

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

Upvotes: 6

Related Questions