'Insert comma after numbers
In this code I'm trying to read multiple files in a folder, and insert a comma after each number. In the first file it works well, but in the second file there are two commas inserted after each number, something like this:
56,, 74,, 2,,
How can I insert just one comma?
input_path = Path(Path.home(), "Desktop", "m")
for root, dirs, files in os.walk(input_path):
for file in files:
file_path = Path(root, file)
with open(file_path) as f:
lines = f.read().splitlines()
with open('C:/--/{}.txt'.format(file), "w") as f:
for line in lines:
f.write(line + ",\n")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
