I would like to match and substitute whitespace that appears in the first line of a CSV.
For example I want to substitute whitespace from the first line only with '_':
"product id","Region","Region Code"
"888","North America","GEO123"
To give:
"product_id","Region","Region_Code"
"888","North America","GEO123"
This is my current approach:
f1 = open('file1', 'r')
f2 = open('newfile', 'w')
for line in f1:
f2.write(re.sub('([\s])+', '_', line))
f1.close()
f2.close()
Which replaces all whitespace throughout the document. How can I adapt this so that it only works on the first line of text?
Aucun commentaire:
Enregistrer un commentaire