I have a text file of names, all of which have three spaces at the end of them, which I would like to remove. When I print these names in python, I get output like the follows:
Adeline Panella  Winifred Aceto  See Weckerly  Daniell Hildebrand  Betsey Coulter  #there are about 1000 of these names To remove the extra spaces, I wrote the following script:
import os script_directory = os.path.dirname(__file__) file = open(os.path.join(script_directory, "assets/data/names.txt"), 'r') potential_names = file.read().splitlines() potential_names = list(filter(None, potential_names)) for item in potential_names: print(item) item = item[:-3] print(item) file.close() file = open(os.path.join(script_directory, "assets/data/names.txt"), 'w') for item in potential_names: file.write("{}\n".format(item)) file.close() It appears to function as expected, as the output is as follows:
Adeline Panella  Adeline Panella Winifred Aceto  Winifred Aceto See Weckerly  See Weckerly Daniell Hildebrand  Daniell Hildebrand Betsey Coulter  Betsey Coulter HOWEVER: When I run the script a second time, the output is exactly the same, and when I examine the text file, the three spaces at the end remain there. How can I permanently remove this extra spacing?