Trying_hard
Trying_hard

Reputation: 9501

Python dropping white space

I am using a csv file that I am iterating over each line and make each column a different title.

for i in csvreader:
    1 = i[0]
    2 = i[1]

etc.

however the 2 column has spaces after the text in the column 'text '. I need to removed the spaces after the text. I need 'text', with no spaces at the end.

I know I could just remove it from the CSV in excel, but I need to do this with Python I am not wanting to open CSV file and work with it.

I have tried to replace and strip, but I can't get this to work. Any suggestions?

Upvotes: 1

Views: 256

Answers (2)

Jon Clements
Jon Clements

Reputation: 142156

Just use:

with open('some.csv') as fin:
    csvin = csv.reader(fin):
    for row in csvin:
        cols = [col.strip() for col in row]
        pass # whatever

Upvotes: 0

NPE
NPE

Reputation: 500367

Use strip() (or lstrip() or rstrip()):

col2 = i[1].strip()

Upvotes: 3

Related Questions