Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add output delimiter option #MERGE-AFTER-INPUT-DELIMITER-PR #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# lcNrD v1.6
# lcNrD v1.7

lowercase AND remove Duplicates - A Very Simple Program;

I find myself having to clean up data quite often & lose python scripts that I write for data cleaning. So I've decided to start this lcNrD project that is a but more of a permanent solution & I will add to its usefulness as I see fit.

## Disclaimer:
As it stands, this program works best with text listed using the '\n' delimiter. I plan to soon add more delimiter options

## Current Functionality:
* Set input delimiter
* Set output delimiter
* Lowercase the text of a file
* Remove duplicates of a file
* Copy all operations onto a new file
Expand All @@ -25,7 +24,12 @@ As it stands, this program works best with text listed using the '\n' delimiter.
treatment (default: None)
-o OUT, --out OUT this is the file you want the lcNrD file to output as.
Default = same name + _LcNrD (default: None)
-l, --lowercase add -l to set lowercase to true (default: False)
-id INPUT_DELIMITER, --input_delimiter INPUT_DELIMITER
this is the delimiter you want lcNrD to use for the
input file (default: \n)
-od OUTPUT_DELIMITER, --output_delimiter OUTPUT_DELIMITER
this is the delimiter you want lcNrD to use for the
output file. Default = input delimiter (default: None)
-d, --duplicates add -d to remove duplicate elements (default: False)
-s, --shuffle add -s to shuffle your list (default: False)
-rf, --replace_file add -rf to replace the original file after lcnrd
Expand Down Expand Up @@ -71,5 +75,3 @@ If you'd like to be able to access lcnrd from anywhere on your computer, add the
7. You should now from anywhere on your computer be able to type simply `lcnrd -h` from a terminal & it should work!

## Roadmap:
* Add input delimiter
* Add output delimiter
17 changes: 15 additions & 2 deletions lcNrD.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
help='** this is the file you\'re targeting to get the lcNrD treatment')
parser.add_argument("-o", "--out", type=str,
help='this is the file you want the lcNrD file to output as. Default = same name + _LcNrD')
parser.add_argument("-id", "--input_delimiter", type=str, default='\\n',
help='this is the delimiter you want lcNrD to use for the input file')
parser.add_argument("-od", "--output_delimiter", type=str,
help='this is the delimiter you want lcNrD to use for the output file. Default = input delimiter')

# store true or false so we can just be like 'if args.lowercase:'
# so use true or false values instead of comparing strings
Expand Down Expand Up @@ -41,6 +45,15 @@
help='add a -kae to add some text to the end of every line')
args = parser.parse_args()

# check if input_delimiter is set to default & if so rectify escape character
# this escapse character is left in so that the default value displays correctly in -h
if(args.input_delimiter == "\\n"):
args.input_delimiter = "\n"

# set output delimiter to input delimiter if output delimiter isn't set
if(args.output_delimiter == None):
args.output_delimiter = args.input_delimiter

print('Grabbing file contents...')

# open the target file & grab its contents. Using `with` like this automatically
Expand All @@ -49,7 +62,7 @@
text = fp.read()

# let's create an array of the lines
lines = text.split("\n")
lines = text.split(args.input_delimiter)

# Remove blank lines
if args.blank_lines:
Expand Down Expand Up @@ -251,6 +264,6 @@
# its creating a brand new one. This gets quickly gets slow with lots of text
# so we wanna do this as little as possible. Using join on the list means
# we just make all the string concatenation opertations in one go.
fp.write("\n".join(lines))
fp.write(args.output_delimiter.join(lines))

print('...& DONE!')