[英]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.我想从 csv 中读取 Pandas dataframe,其中列名中有单个空格,分隔符是多个空格。 How can I tell Pandas to use only more than one consecutive whitespace as separator but ignore single whitespaces?
如何告诉 Pandas 仅使用多个连续空格作为分隔符,但忽略单个空格?
With specific regex pattern for sep=
option:使用
sep=
选项的特定正则表达式模式:
df = pd.read_csv(sep='\s{2,}')
\s{2,}
- quantifier, matches any whitespace character between 2
and unlimited times, as many times as possible \s{2,}
- 量词,匹配2
次到无限次之间的任何空白字符,尽可能多次 another option that I actually use, which saves me some shift
keypresses:我实际使用的另一个选项,它为我节省了一些
shift
按键:
df = pd.read_csv('file.csv', sep='\s\s+')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.