英文:
An undefined letter appears?
问题
我运行以下代码片段。由于某种原因,我无法理解,在指定文件位置时,路径中出现了一个“b”。
# %% 定义
path_root = normpath(r'G:\Dropbox\temp\')
path_start = normpath(r'G:\Dropbox\temp\worklayers\')
path_temp = normpath(r'G:\Dropbox\temp\templayers\')
path_temp2 = normpath(r'G:\Dropbox\temp\temp2layers\')
path_end = normpath(r'G:\Dropbox\temp\resultlayers\')
path_doc = normpath(r'G:\Dropbox\Documents\')
directory = os.fsencode(path_start)
df = pd.read_csv(path_doc+'\\DEM_masterfile.csv')
slope_list = [4, 4.9, 5.9, 7.1, 8.2, 10, 11.8, 13.8, 15.7, 17.9, 20.6, 25]
aspect_list_1 = [[0, 15], [15, 30], [30, 45], [45, 60], [60, 75], [75, 90], [90, 105], [105, 120], [120, 135], [135, 150], [150, 165], [165, 180]]
aspect_list_2 = [[180, 195], [195, 210], [210, 225], [225, 240], [240, 255], [255, 270], [270, 285], [285, 300], [300, 315], [315, 330], [330, 345], [345, 360]]
# %% 从DEM栅格生成坡度和坡向向量文件
for number in os.listdir(directory):
processing.run("native:slope", {'INPUT':path_start+'\\'+str(number),'Z_FACTOR':1,'OUTPUT':path_temp+'\\'+'slope_'+str(number)})
processing.run("native:aspect", {'INPUT':path_start+'\\'+str(number),'Z_FACTOR':1,'OUTPUT':path_temp+'\\'+'aspect_'+str(number)})
当运行时,出现以下错误消息:“无法加载输入的源图层:G:\Dropbox\temp\worklayers\b'hej0.tif' 未找到”。
这个错误消息中的“b”是从哪里来的?G:\Dropbox\temp\worklayers\b'hej0.tif'
英文:
I'm running the following snippet. For some reason, which I don't get, a "b" appears in the path when stating the location of a file.
# %% Definitions
path_root = normpath(r'G:\\Dropbox\\temp\\')
path_start = normpath(r'G:\\Dropbox\\temp\\worklayers\\')
path_temp = normpath(r'G:\\Dropbox\\temp\\templayers\\')
path_temp2 = normpath(r'G:\\Dropbox\\temp\\temp2layers\\')
path_end = normpath(r'G:\\Dropbox\\temp\\resultlayers\\')
path_doc = normpath(r'G:\\Dropbox\\Documents\\')
directory = os.fsencode(path_start)
df = pd.read_csv(path_doc+'\\DEM_masterfile.csv')
slope_list = [4, 4.9, 5.9, 7.1, 8.2, 10, 11.8, 13.8, 15.7, 17.9, 20.6, 25]
aspect_list_1 = [[0, 15], [15,30], [30, 45], [45, 60], [60, 75], [75, 90], [90, 105], [105, 120], [120, 135], [135, 150], [150, 165], [165, 180]]
aspect_list_2 = [[180, 195], [195, 210], [210, 225], [225, 240], [240, 255], [255, 270], [270, 285], [285, 300], [300, 315], [315, 330], [330, 345], [345, 360]]
# %% Generating the slope and aspect vector files from the DEM raster
for number in os.listdir(directory):
processing.run("native:slope", {'INPUT':path_start+'\\'+str(number),'Z_FACTOR':1,'OUTPUT':path_temp+'\\'+'slope_'+str(number)})
processing.run("native:aspect", {'INPUT':path_start+'\\'+str(number),'Z_FACTOR':1,'OUTPUT':path_temp+'\\'+'aspect_'+str(number)})
When running this the following error appears Could not load source layer for INPUT: G:\Dropbox\temp\worklayers\b'hej0.tif' not found
Where is that b in the error message coming from?? G:\Dropbox\temp\worklayers\b'hej0.tif'
答案1
得分: 2
os.fsencode(path_start)
返回一个 bytes
实例。然后 os.listdir(directory)
返回一个 bytes
列表,因为你将 bytes
作为其参数。
b'...'
是在 bytes
实例上调用 str(...)
的结果。我不确定在这段代码中使用 os.fsencode
最初的原因,导致最终得到 bytes
。
要将 bytes
转换为字符串,你可以使用 decode()
或 os.fsdecode
,它是 os.fsencode
的反向操作。
英文:
os.fsencode(path_start)
returns an instance of bytes
.
Then os.listdir(directory)
returns a list of bytes
because you gave bytes
as its argument.
The b'...'
is the result of calling str(...)
on an instance of bytes
.
I'm not sure what the reason was for using os.fsencode
in this code and ending up with bytes
.
To convert a bytes
to a string, you could use decode()
or os.fsdecode
, the reverse of os.fsencode
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论