简体   繁体   English

消除pd.read_csv中的空格

[英]eliminate whitespaces in pd.read_csv

lets say I have a csv file with lines in following format. 可以说我有一个csv文件,其中的行格式如下。

89.96.146.2 # Some String Related,To,45.53,11.0 89.96.146.2#与某些字符串相关,到,45.53,11.0

I want to read these lines in to a pandas dataframe and perform some search function based on the IP address(89.96.146.2). 我想将这些行读入pandas数据框中,并基于IP地址(89.96.146.2)执行一些搜索功能。

df = pd.read_csv('test.csv', sep='#\s+', header=None).set_index(0)

This has a white space with IP?. IP带有空格? I can only perform the the search function if I split this as line.split(' ')[0] . 如果将其拆分为line.split(' ')[0]则只能执行搜索功能。 Why my above code do not eliminate that white space? 为什么我上面的代码没有消除空白?

我想您需要说的是#号之前有空格:

pd.read_csv('test.csv', sep='\s+#\s+', header=None).set_index(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM