Parse a single SQL value string into Python type.
(v)
| 96 | |
| 97 | |
| 98 | def parse_sql_value(v): |
| 99 | """Parse a single SQL value string into Python type.""" |
| 100 | v = v.strip() |
| 101 | if v == 'NULL': |
| 102 | return None |
| 103 | if (v.startswith("'") and v.endswith("'")) or (v.startswith('"') and v.endswith('"')): |
| 104 | s = v[1:-1] |
| 105 | s = s.replace("\\'", "'").replace('\\"', '"').replace('\\\\', '\\') |
| 106 | s = s.replace('\\n', '\n').replace('\\r', '\r').replace('\\t', '\t') |
| 107 | # Handle BOM at start of first value |
| 108 | if s.startswith('\ufeff'): |
| 109 | s = s[1:] |
| 110 | return s |
| 111 | try: |
| 112 | return int(v) |
| 113 | except ValueError: |
| 114 | pass |
| 115 | try: |
| 116 | return float(v) |
| 117 | except ValueError: |
| 118 | pass |
| 119 | return v |
| 120 | |
| 121 | |
| 122 | def parse_insert_row(line, col_names): |