Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove spaced before and after commas in a csv file using python?

Tags:

python

regex

csv

Currently one row of my csv file looks like this :

314523, 165538, 76255, 335416, 416827 1250536:1 1744638:1 298526:1 1568238:1

I need it to look like this :

314523,165538,76255,335416,416827 1250536:1 1744638:1 298526:1 1568238:1

I only want to remove the spaces after or before commas and leave the other blank spaces as it is.

How can I do this in python?

Note : I am a beginner in python

like image 655
user324 Avatar asked Oct 17 '25 09:10

user324


1 Answers

I would recommend using the replace function. You enter the pattern you want to replace. In your example, the pattern is comma space (', ') and space comma (' ,'). Then say what you want to replace the pattern with (',').

line=line.replace(', ', ',').replace(' ,',',')
like image 73
goCards Avatar answered Oct 19 '25 22:10

goCards



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!