user1082748
user1082748

Reputation: 365

python AttributeError: 'NoneType' object has no attribute 'replace' in list

#@ 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?

Upvotes: 0

Views: 5114

Answers (2)

quamrana
quamrana

Reputation: 39404

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

Upvotes: 1

Veera Balla Deva
Veera Balla Deva

Reputation: 788

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'

Upvotes: 0

Related Questions