简体   繁体   中英

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?

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

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

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

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