python - Strip carriage returns / line feeds from database records before they are written to csv -
i have following code reads lines database , writes matching results csv file. problem running there carriage returns / line feeds in of fields in different rows cause csv file unusable because there bogus rows.
for example, here sample of happens when there carriage returns / line feeds in sql data , affect has on file. ... sample content of messed file:
field1|field2|field3|field4|field5 value 1|value 2|value 3|value 4|value 5 value 1|value 2|value 3|value 4|value 5 value 1|value 2|val ue 3|value 4|value 5 value 1|value 2|value 3|va lue 4|value 5
here code writes results of sql query output file. trying strip results have carriage returns / line feeds.
''' while loop read each row. if compares row[2] (updated) against last record processed ''' latest = params #declare 'latest' variable consumption while loop while row: if row[2] > latest: latest = row[2] logger.debug("[%s] - writing row %s", correlationid, row) writer.writerow(row) row = cursor.fetchone() logger.info("[%s] - last letter date %s " % (correlationid, lastprocessed)) lastprocessedlog = open(last_processed_logfile , 'wt') lastprocessedstring = str(latest) lastprocessedstring = lastprocessedstring[0:19] lastprocessedlog.write(lastprocessedstring) lastprocessedlog.close() conn.close() ofile.close() logger.info("[%s] - copying %s root loadbackflow %s", correlationid, writefile, outfile) shutil.copyfile(writefile, outfile) logger.info("[%s] - moving %s completion folder %s", correlationid, writefile, completionfolder) shutil.move(writefile, completionfolder)
i have tried changing writer.write(row) line include replace error. similarly, errors when trying use replace row = row.replace("\r\n", "") ... have pasted attempts , corresponding errors below.
any insights on how can strip carriage returns / line feeds @ time being read sql query results data file appreciated.
thanks in advance! :)
# attempt1: writer.writerow(row).replace("\r\n", "") # error: unexpected error: 'nonetype' object has no attribute 'replace' # attempt2: row = row.replace("\r\n", "") #error: unexpected error: 'tuple' object has no attribute 'replace' #attempt3: row = row.replace("\r", "") row = row.replace("\n", "") #error: unexpected error: 'tuple' object has no attribute 'replace'
programming permutation known antipattern... db.cursor.fetchone()
returns tuple
, - error message tells - has no replace()
method (what semantic of (1, 2, 3).replace("a string", "another string")
?), , csv.writer.writerow()
return none
(and since have written row, point of trying modify afterward anyway ?).
so, make long story short, have 2 options:
- modify the strings in row before passing
writerow()
- use csv format allow escape newlines
i don't know how plan on using generated csv, solution #2 still best approach if possible - these newlines might here reason - , requires passing right arguments when instanciating csv.writer
.
if that's definitly not option, solution #1 needs bit more work, tuples , strings immutable:
def preprocess_item(item): if isinstance(item, str): return item.replace("\n", " ").replace("\r", " ") return item def preprocess_row(row): return tuple(preprocess_item(item) item in row) def yourfunction(whatever): ### code here row = cursor.fetchone() row = preprocess_row(row) writer.writerow(row)
Comments
Post a Comment