0
#@ Row 1, Column 4: Invalid character value for cast specification @#

BCC,01,12697,2013-12- 12,1.0,2014004,CR,ACCOUN TS PAYABLE,-86.23000,,2200-000-000,, ,,,,True,,0,False,,,False,,,,,,0.00000,0.00000

I am trying to remove the tab in the date value and elsewhere in each row of data.

columndata = [str(items.replace('\t', '')) for items in list(row)]

However, this command returns the following error:

  File "apdetfac.py", line 60, in <listcomp>
  columndata = [str(items.replace('\t', '')) for items in list(row)]
  AttributeError: 'NoneType' object has no attribute 'replace'

I tried converting items to str as follows str(items) in list(row) but that generated another error. What to do?

11
  • 1
    there might be missing values in your date column. Try [str(items.replace('\t', '')) if items else items for items in list(row)] Also, what is the row object? Is it 1 row of data? In that case you are not looping properly Commented Feb 6, 2018 at 12:09
  • give your full code. Commented Feb 6, 2018 at 12:12
  • It is data from Progress extracted using pyodbc. Im looping on for row in data: Commented Feb 6, 2018 at 12:13
  • I will try your suggestion Vivek Commented Feb 6, 2018 at 12:14
  • This is what I get now: AttributeError: 'bool' object has no attribute 'replace' Commented Feb 6, 2018 at 12:15

2 Answers 2

1

Its difficult to tell what data you have, but this gives the right sort of answer:

row_str = 'BCC,01,12697,2013-12-\t12,1.0,2014004,CR,ACCOUN\tTS PAYABLE,-86.23000,,2200-000-000,, ,,,,True,,0,False,,,False,,,,,,0.00000,0.00000'
# note the '\t' in date and ACCOUNTS

row = row_str.split(',')

columndata = [str(items.replace('\t', '')) for items in row]

print(columndata)

Output:

['BCC', '01', '12697', '2013-12-12', '1.0', '2014004', 'CR', 'ACCOUNTS PAYABLE', '-86.23000', '', '2200-000-000', '', ' ', '', '', '', 'True', '', '0', 'False', '', '', 'False', '', '', '', '', '', '0.00000', '0.00000']

Of course this list can be joined back into a string:

new_row = ','.join(columndata)
print(new_row)

Output:

BCC,01,12697,2013-12-12,1.0,2014004,CR,ACCOUNTS PAYABLE,-86.23000,,2200-000-000,, ,,,,True,,0,False,,,False,,,,,,0.00000,0.00000
Sign up to request clarification or add additional context in comments.

3 Comments

rowStr = row.split(',') AttributeError: 'pyodbc.Row' object has no attribute 'split'
I converted row.split to str(row).split and rest of remains the same and it appears to be working.
Will you all know in a few once the million plus rows are processed. thanks for your help.
0
data = "'BCC,01,12697,2013-12-\t12,1.0,2014004,CR,ACCOUN\tTS PAYABLE,-86.23000,,2200-000-000,, ,,,,True,,0,False,,,False,,,,,,0.00000,0.00000'"

print(','.join(map(lambda x: x.replace('\t',''), data.split(','))))
>>>'BCC,01,12697,2013-12-12,1.0,2014004,CR,ACCOUNTS PAYABLE,-86.23000,,2200-000-000,, ,,,,True,,0,False,,,False,,,,,,0.00000,0.00000'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.