2016-11-22 27 views
-9

,我想下面的字符串转换:的Python:如何切串起来,用它的一部分,并丢弃他人Python3

bpy.types.object.location.* std:label -1 editors/3dview/object/properties/transforms.html#bpy-types-object-location Transform Properties

对此以下字符串:

("bpy.types.Object.location.*", "editors/3dview/object/properties/transforms.html"),

目前,我只有:

with open("objects.tmp") as f: 
    for line in f: 
     if "bpy.types." in line: 
      fw (' ("' + line.rstrip() +'")\n') 

如何在特定点处切断字符串*或/和X空格后?

+4

通过做一个教程和阅读文档,然后尝试它自己...此外,您显示的所需输出不是一个字符串。 – schwobaseggl

+0

假设你所有的字符串都遵循类似的模式,你可以试试:'tuple(string.split()[0],string.split()[3])'...但这真的很容易出错。你目前有什么代码? – blacksite

+1

你需要解析它。什么决定保留什么?你有什么尝试? – Carcigenicate

回答

0
originalString = 'bpy.types.object.location.* std:label -1 editors/3dview/object/properties/transforms.html#bpy-types-object-location Transform Properties' 

# this separates your string by spaces 
split = originalString.split(' ') 

# first element is ready to go! 
first_element = split[0] # 'bpy.types.object.location.*' 

# second element needs to be cut at "#" 
second_element = split[3] 
second_element = second_element[:second_element.find('#')] # editors/3dview/object/properties/transforms.html 

# now you have your desired output 
output = tuple(first_element, second_element) 
相关问题