消除pd.read_csv中的空白 [英] eliminate whitespaces in pd.read_csv
本文介绍了消除pd.read_csv中的空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以说我有一个带有以下格式的csv文件。
lets say I have a csv file with lines in following format.
89.96.146.2#一些字符串相关,To,45.53, 11.0
89.96.146.2 # Some String Related,To,45.53,11.0
我想将这些行读入熊猫数据帧,并根据IP地址执行一些搜索功能(89.96.146.2) 。
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).
df = pd.read_csv('test.csv', sep='#\s+', header=None).set_index(0)
这有一个空白的IP?我只能执行搜索功能,如果我把它分成 line.split('')[0]
。为什么我的上面的代码不能消除这个空白?
This has a white space with IP?. I can only perform the the search function if I split this as line.split(' ')[0]
. Why my above code do not eliminate that white space?
推荐答案
我想你需要说的是在标签之前有空格:
I suppose you need to say that there are spaces before the hashtag:
pd.read_csv('test.csv', sep='\s+#\s+', header=None).set_index(0)
这篇关于消除pd.read_csv中的空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文