I need to perform this operation to every single line in a text file and then write it to another file. I need to remove the first whitespace and replace it with a comma (,
).
However, I don't want this to happen to any other whitespace on the line. Here is a snippet from the input text file:
Achilles Patroclus
Achilles Triumph of Achilles in Corfu Achilleion
Achilles Antilochus
Achilles Hephaestus
Achilles Shield of Achilles
You can do something like this:
[",".join(line.split(" ", 1)) for line in lines]
Using re.sub
with ^([^\s]*)\s+
:
>>> s
'Achilles Triumph of Achilles in Corfu Achilleion'
>>> re.sub(r'^([^\s]*)\s+', r'\1, ', s)
'Achilles, Triumph of Achilles in Corfu Achilleion'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With