简体   繁体   English

训练 '\\x00' s 和 TypeError: stat() 参数 1 必须是没有空字节的编码字符串,而不是 str

[英]Training '\x00' s and TypeError: stat() argument 1 must be encoded string without null bytes, not str

I have a json file where I stored path to several files like this:我有一个 json 文件,我在其中存储了几个文件的路径,如下所示:

>> head test_data.json 
data/Benign/P_00004_LEFT_CC.tif
data/Benign/P_00004_LEFT_MLO.tif
data/Benign/P_00004_RIGHT_MLO.tif
data/Benign/P_00018_RIGHT_CC.tif

Now the problem is when I read the file in python and save each line in an array I have trailing \\x00\\x00\\x00\\x00\\x00 at the end of line.现在的问题是当我在 python 中读取文件并将每一行保存在一个数组中时,我在行尾有尾随\\x00\\x00\\x00\\x00\\x00 I tried str.rstrip() and str.rstrip('\\n') but had no luck.我试过str.rstrip()str.rstrip('\\n')但没有运气。

在此处输入图片说明

This is causing problem when I try to check the existence of paths using os.path.exists() like following当我尝试使用os.path.exists()检查路径是否存在时,这会导致问题,如下所示

if os.path.exists(path_list[0]):
       img1 = self.loader(path_list[0])
        ........

I am getting error at os.path.exists()我在os.path.exists()出现错误

TypeError: stat() argument 1 must be encoded string without null bytes, not str

How can I resolve this issue?我该如何解决这个问题?

You can use rstrip('\\x00') , or you can use rstrip('\\x00\\n') to strip both '\\n' and \\x00' concurrently:您可以使用rstrip('\\x00') ,也可以使用rstrip('\\x00\\n')同时剥离'\\n'\\x00'

>>> s = 'data/Benign/P_00004_LEFT_CC.tif\x00\x00\x00\x00\x00\n'
>>> s.rstrip('\n\x00')
'data/Benign/P_00004_LEFT_CC.tif'

That being said, I would ask myself how NULL-characters occur in the file in the first place.话虽如此,我首先会问自己文件中是如何出现 NULL 字符的。

You can process the list of path_list in batch by using:您可以使用以下命令批量处理path_list的列表:

path_list = [item.rstrip('\n\x00') for item in path_list]

before using the path_list when processing the file names.在处理文件名时使用path_list之前。

暂无
暂无

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

相关问题 TypeError:file()参数1必须是没有NULL字节的编码字符串,而不是str - TypeError: file() argument 1 must be encoded string without NULL bytes, not str TypeError:必须是不包含空字节的字符串,而不是str - TypeError: must be string without null bytes, not str python TypeError:必须是没有 NULL 字节的编码字符串,而不是 str - docker iron requirements.txt - python TypeError: must be encoded string without NULL bytes, not str - docker iron requirements.txt TypeError:必须是不包含空字节的字符串,在os.system()中不能为str - TypeError: must be string without null bytes, not str in os.system() 编码:TypeError:write()参数必须是str,而不是bytes - Encoding: TypeError: write() argument must be str, not bytes 类型错误:join() 参数必须是 str 或 bytes,而不是“dict” - TypeError: join() argument must be str or bytes, not 'dict' 类型错误:join() 参数必须是 str 或字节,而不是“PosixPath” - TypeError: join() argument must be str or bytes, not 'PosixPath' `TypeError:strptime()参数0必须是str,而不是 <class 'bytes'> ` - `TypeError: strptime() argument 0 must be str, not <class 'bytes'>` Python - 类型错误:write() 参数必须是 str,而不是字节 - Python - TypeError: write() argument must be str, not bytes TypeError: join() 参数必须是 str 或 bytes,而不是 'list' - TypeError: join() argument must be str or bytes, not 'list'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM