Need to skip line containing “Value Error”

大憨熊 提交于 2019-12-11 12:56:45

问题


I'm trying to extract some legacy data from a Teradata server, but some of the records contain weird characters that don't register in python, such as "U+ffffffc2".

Currently,

  1. I'm using pyodbc to extract the data from Teradata

  2. Placing the results into a numpy array (because when I put it directly into pandas, It interprets all of the columns as a single column of type string)

  3. Then I turn the numpy array into a pandas dataframe to change things like Decimal("09809") and Date("2015,11,14") into [09809,"11,14,2015"]

  4. Then I try to write it to a file, where this error occurs

    ValueError: character U+ffffffc2 is not in range [U+0000; U+10ffff]

I don't have access to edit this data, so from a client perspective what can I do to skip or, preferably, remove the character before writing it trying to write it to a file and getting the error?

Currently, I have a "try and except" block to skip queries with erroneous data, but I have to query the data in row chunks of at least 100. So if I just skip it, I lose 100 or more lines at a time. As I mentioned before, however, I would prefer to keep the line, but remove the character.

Here's my code. (Feel free to point out any bad practices as well!)

#Python 3.4
 #Python Teradata Extraction
 #Created 01/28/16 by Maz Baig

 #dependencies
 import pyodbc
 import numpy as np
 import pandas as pd
 import sys
 import os
 import psutil
 from datetime import datetime


 #create a global variable for start time
 start_time=datetime.now()
 #create global process variable to keep track of memory usage
 process=psutil.Process(os.getpid())

 def ResultIter(curs, arraysize):
         #Get the specified number of rows at a time
         while True:
                 results = curs.fetchmany(arraysize)
                 if not results:
                         break
                 #for result in results:
                 yield results

 def WriteResult(curs,file_path,full_count):
         rate=100
         rows_extracted=0
         for result in ResultIter(curs,rate):
                 table_matrix=np.array(result)
                 #Get shape to make sure its not a 1d matrix
                 rows, length = table_matrix.shape
                 #if it is a 1D matrix, add a row of nothing to make sure pandas doesn't throw an error
                 if rows < 2:
                         dummyrow=np.zeros((1,length))
                         dummyrow[:]=None
                 df = pd.DataFrame(table_matrix)
                 #give the user a status update
                 rows_extracted=rows+rows_extracted
                 StatusUpdate(rows_extracted,full_count)
                 with open(file_path,'a') as f:
                         try:
                                 df.to_csv(file_path,sep='\u0001',encoding='latin-1',header=False,index=False)
                         except ValueError:
                                 #pass afterwards
                                 print("This record was giving you issues")
                                 print(table_matrix)
                                 pass
         print('\n')
         if (rows_extracted < full_count):
                 print("All of the records were not extracted")
                 #print the run durration
                 print("Duration:  "+str(datetime.now() - start_time))
                 sys.exit(3)
         f.close()




 def StatusUpdate(rows_ex,full_count):
         print("                                      ::Rows Extracted:"+str(rows_ex)+" of "+str(full_count)+"    |    Memory Usage: "+str(process.memory_info().rss/78



 def main(args):
         #get Username and Password
         usr = args[1]
         pwd = args[2]
         #Define Table
         view_name=args[3]
         table_name=args[4]
         run_date=args[5]
         #get the select statement as an input
         select_statement=args[6]
         if select_statement=='':
                 select_statement='*'
         #create the output filename from tablename and run date
         file_name=run_date + "_" + table_name +"_hist.dat"
         file_path="/prod/data/cohl/rfnry/cohl_mort_loan_perfnc/temp/"+file_name
         if ( not os.path.exists(file_path)):
                 #create connection
                 print("Logging In")
                 con_str = 'DRIVER={Teradata};DBCNAME=oneview;UID='+usr+';PWD='+pwd+';QUIETMODE=YES;'
                 conn = pyodbc.connect(con_str)
                 print("Logged In")

                 #Get number of records in the file
                 count_query = 'select count (*) from '+view_name+'.'+table_name
                 count_curs = conn.cursor()
                 count_curs.execute(count_query)
                 full_count = count_curs.fetchone()[0]

                 #Generate query to retrieve all of the table data
                 query = 'select '+select_statement+'  from '+view_name+'.'+table_name
                 #create cursor
                 curs = conn.cursor()
                 #execute query
                 curs.execute(query)
                 #save contents of the query into a matrix
                 print("Writting Result Into File Now")
                 WriteResult(curs,file_path,full_count)
                 print("Table: "+table_name+" was successfully extracted")
                 #print the scripts run duration
                 print("Duration:  "+str(datetime.now() - start_time))
                 sys.exit(0)
         else:
                 print("AlreadyThere Exception\nThe file already exists at "+file_path+". Please remove it before continuing\n")
                 #print the scripts run duration
                 print("Duration:  "+str(datetime.now() - start_time))
                 sys.exit(2)

 main(sys.argv)

Thanks,

Maz


回答1:


If you have only 4-byte unicode points giving an error, this probably may help. One solution is to register a custom error handler using codecs.register_error, which would filter out error points and then just try to decode:

import codecs

def error_handler(error):
    return '', error.end+6

codecs.register_error('nonunicode', error_handler)

b'abc\xffffffc2def'.decode(errors='nonunicode')
# gives you 'abcdef' which's exactly what you want

You may futher impove your handler to catch more complicated errors, see https://docs.python.org/3/library/exceptions.html#UnicodeError and https://docs.python.org/3/library/codecs.html#codecs.register_error for details



来源:https://stackoverflow.com/questions/35162318/need-to-skip-line-containing-value-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!