简体   繁体   English

python AttributeError:“ NoneType”对象在列表中没有属性“ replace”

[英]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 BCC,01,12697,2013-12- 12,1.0,2014004,CR,ACCOUN TS PAYABLE,-86.23000,,2200-000-000 ,,,,,,真,, 0,假,,,假,,, ,,, 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. 我尝试将项目转换为str(如下)在list(row)中的str(items),但这产生了另一个错误。 What to do? 该怎么办?

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
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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:“AttributeError:'NoneType' object 没有属性'replace'” - Python: "AttributeError: 'NoneType' object has no attribute 'replace' " AttributeError:&#39;NoneType&#39;对象没有属性&#39;replace&#39; - AttributeError: 'NoneType' object has no attribute 'replace' AttributeError:“ NoneType”对象没有属性“ replace_with” - AttributeError: 'NoneType' object has no attribute 'replace_with' 带有 AttributeError 的函数:“NoneType”object 没有属性“replace” - Functions with AttributeError: 'NoneType' object has no attribute 'replace' AttributeError: 'list' object 没有属性 'replace' Python - AttributeError: 'list' object has no attribute 'replace' Python Python,AttributeError:“ NoneType”对象没有属性“ show” - Python, AttributeError: 'NoneType' object has no attribute 'show' Python:AttributeError:&#39;NoneType&#39;对象没有属性&#39;findNext&#39; - Python: AttributeError: 'NoneType' object has no attribute 'findNext' Python AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;fileno&#39; - Python AttributeError: 'NoneType' object has no attribute 'fileno' Python:AttributeError:&#39;NoneType&#39;对象没有属性&#39;groups&#39; - Python: AttributeError: 'NoneType' object has no attribute 'groups' Python:AttributeError:&#39;NoneType&#39;对象没有属性&#39;type&#39; - Python: AttributeError: 'NoneType' object has no attribute 'type'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM