我有以下清单:
>>> poly
'C:\\04-las_clip_inside_area\\16x16grids_1pp_fsa.shp'
>>> record
1373155
我希望创造:
'C:\\04-las_clip_inside_area\\16x16grids_1pp_fsa_1373155.txt'
为了得到“C:\ 04-las_clip_inside_area\16x16grids_1pp_fsa16x16grids_1pp_fsa”部分,我希望分割。
我尝试过以下两种代码行解决方案:
mylist = [poly.split(".")[0], "_", record, ".txt"]
>>> mylist
['C:\\04-las_clip_inside_area\\16x16grids_1pp_fsa', '_', 1373155, '.txt']
从这里开始,阅读Python join, why is it string.join(list) instead of list.join(string)?中的示例。
我找到了joint的解决方案,但是我得到了一条错误消息:
>>> mylist.join("")
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
AttributeError: 'list' object has no attribute 'join'
如果我使用:
>>> "".join(mylist)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: sequence item 2: expected string, int found
Python join: why is it string.join(list) instead of list.join(string)?
所以有
而不是
这是你的错误。
要解决
int/string
问题,请将int转换为字符串:或者直接写:
或者如果你坚持使用
join()
使用
rpartition()
(或rsplit()
)很重要,否则如果路径中有任何其他的'.'
,它将无法正常工作相关问题 更多 >
编程相关推荐