Skip to content

Instantly share code, notes, and snippets.

@freejoe76
Last active August 29, 2015 14:04
Show Gist options
  • Save freejoe76/78d76fe4ffa00549dac3 to your computer and use it in GitHub Desktop.
Save freejoe76/78d76fe4ffa00549dac3 to your computer and use it in GitHub Desktop.
Delete The Last Comma
#!/usr/bin/env python
#!/usr/bin/env python
# Deletes the last comma from a string.
# We don't know if the comma will be at the end or near the end of a string.
from optparse import OptionParser
import os.path
import types
def delete_comma(value):
""" Returns a string without its final comma."""
items = value.split(',')
last = items.pop()
return '%s%s' % (','.join(items), last)
if __name__ == '__main__':
parser = OptionParser()
(options, args) = parser.parse_args()
# Check if we're operating on the contents of a file
if os.path.isfile(args[0]):
fn = open(args[0], 'r')
content = fn.read()
fn.close
fn = open(args[0], 'w')
if type(content) is not types.UnicodeType:
content = content.decode('utf-8', 'ignore')
content = delete_comma(content)
fn.write(content.encode('utf-8', 'ignore'))
fn.close
else:
print delete_comma(' '.join(args))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment