首页 > 解决方案 > 无法更改 numpy 数组中字符串的大小

问题描述

我在更改存储在 NumPy 数组中的字符串的大小时遇到​​问题。例如,我将图像的路径存储在一个数组中,并使用“replace”函数将末尾的“.png”替换为“ something .png”。但是发生的情况是该操作仅将新字符串保持在字符串的原始长度,不会添加任何额外的内容。

  1. E:\Thesis Data\2018-weedMap-dataset-release\Tiles\RedEdge\000\groundtruth\000_frame0000.png
  2. E:\Thesis Data\2018-weedMap-dataset-release\Tiles\RedEdge\000\groundtruth\000_frame0000_und

1:原始字符串 2:替换字符串

正如在上面的字符串中看到的那样,我用“_underground.png”替换了“.png”,但它只保留了“_und”,它等于我要替换的字符串的长度,即“.png”。

标签: pythonstringnumpyreplace

解决方案


So your sample string(s) is 91 char long, consistent with the '<U91' dtype. You can't increase that without making a new array with a longer dtype.

In [11]: ''' 1. E:\Thesis
    ...:     Data\2018-weedMap-dataset-release\Tiles\RedEdge\000\groundtruth\000_frame0000.
    ...: png '''
Out[11]: ' 1. E:\\Thesis\n    Data\x818-weedMap-dataset-release\\Tiles\\RedEdge\x00\\groundtruth\x00_frame0000.png '
In [12]: len(_)
Out[12]: 91

推荐阅读