Created
October 1, 2018 19:58
-
-
Save EdMan1022/5e271b2c69d02ac8934de47911581a94 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def upload(line): | |
""" | |
Executes some operation using a dict and returns some output | |
This is where the Eloqua POST function would go, | |
I haven't implemented anything | |
:param line: | |
:return: | |
""" | |
return list(line.values()) | |
def load_row(row_file_path): | |
try: | |
with open(row_file_path, 'r') as row_file: | |
return int(row_file.read()) | |
except FileNotFoundError: | |
return 0 | |
def seek_csv_row(csv_file, row_n): | |
""" | |
Seeks to the given row number of a csv DictReader by calling readline | |
:param csv_file: | |
:param row_n: | |
:return: | |
""" | |
for i in range(row_n): | |
next(csv_file) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from eloqua_upload.helper_functions import upload | |
def loop_function(csv_file, output_file, row_n, row_file_path): | |
try: | |
for line in csv_file: | |
output_file.writerow(upload(line)) | |
row_n += 1 | |
except Exception as e: | |
# If you want to do some operation on the error you could do it here | |
# The way I've written it you'd still want to raise the error and fail | |
# the loop at this point. | |
# | |
# You can resolve the error in the data and start up from here, | |
# since the line n will be equal to this line. | |
# | |
# If you want to write some way to set aside lines with errors, | |
# continue to process the rest of the uploads, | |
# and then handle the error lines separately you could, | |
# just make sure you implement it correctly | |
raise e | |
finally: | |
with open(row_file_path, 'w') as row_file: | |
row_file.write(str(row_n)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
from eloqua_upload.helper_functions import load_row, seek_csv_row | |
from eloqua_upload.loop import loop_function | |
def run_function(input_file_path, output_file_path, row_file_path, operation): | |
""" | |
Sets up file objects and executes operation on each row from input | |
:param input_file_path: The input file path | |
:param output_file_path: Path to output file for executed function | |
:param row_file_path: File that contains row number process ran till | |
:param operation: (callable) Callable to be executed, takes file objects as argument | |
:return: | |
""" | |
row_n = load_row(row_file_path) | |
# I'm using with blocks here so that even an exception in one of the file | |
# paths will still close any open files without corrupting them | |
# I'm opening the input file in read only mode | |
with open(input_file_path, 'r') as input_file: | |
csv_file = csv.DictReader(input_file) | |
# Seek to the correct row | |
seek_csv_row(csv_file=csv_file, row_n=row_n) | |
# i'm opening the output file in append mode, | |
# since we're going to append to the same file even if crashes occur | |
with open(output_file_path, 'a') as output_file: | |
output_csv = csv.writer(output_file) | |
operation(csv_file, output_csv, row_n, row_file_path) | |
if __name__ == '__main__': | |
run_function( | |
input_file_path='/Users/ebrennan/PycharmProjects/eloqua-csv-upload/scrap_input.csv', | |
output_file_path='/Users/ebrennan/PycharmProjects/eloqua-csv-upload/scrap_output.csv', | |
row_file_path='/Users/ebrennan/PycharmProjects/eloqua-csv-upload/row_n.txt', | |
operation=loop_function | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
key0 | key1 | key2 | key3 | key4 | key5 | key6 | key7 | key8 | key9 | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My previous version of the
seek_csv_row
function wouldn't have worked, because it was callingreadline
, which isn't an implemented method for the python CSV package's readers and writers. I fixed it by callingnext()
, which should correctly skip to the desired row number