Nilani Algiriyage
Nilani Algiriyage

Reputation: 35696

eliminate whitespaces in pd.read_csv

lets say I have a csv file with lines in following format.

89.96.146.2 # Some String Related,To,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).

df = pd.read_csv('test.csv', sep='#\s+', header=None).set_index(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?

Upvotes: 2

Views: 2163

Answers (1)

Teudimundo
Teudimundo

Reputation: 2670

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)

Upvotes: 4

Related Questions