简体   繁体   English

字符串切片不接受变量

[英]string slicing not accepting variables

I am writing a short method to slice a string based upon the index of comma's in a different method.我正在编写一个简短的方法来根据逗号的索引以不同的方法对字符串进行切片。 I droped everything into one code for easy viewing.我将所有内容都放在一个代码中以便于查看。

with open(testconfig) as f:
        lines=f.readlines()
        initlist=[]
        string_slice_list=[]
        b=0
        for line in lines:
            if "ML_STARConfig" in line:
                b+=1
                liste=sfind(line,",")
                liste.insert(0,0)
                input("line is {}sfind list of characters is {} and there should be a zero in position 0".format(line,liste))
                liste=list(reversed(liste))
                slices=[]
                for i in range(1,len(liste)):#make a list of tuples that have the beginning and end indexes of a "useful slice" of the string "line"
                    slices.append((liste[i-1],liste[i]))
                for item in slices:
                    b=item[0]
                    e=item[1]
                    word=line[b:e]
                    input("line is {} word indexes are {},{} and this should be a word ={}".format(line,b,e,word))

the method accepts a single line from a file and creates a list of the indexes of the comma's in the line.该方法接受文件中的一行并创建该行中逗号索引的列表。 I assign each pair of indexes to a tuple and append each tuple to a list.我将每对索引分配给一个元组,并将 append 每个元组分配给一个列表。 and then loop over the list of tuples to extract each of the words of interest from the line.然后遍历元组列表以从行中提取每个感兴趣的单词。 Except when I pass the numbers from the list of tuples it will not slice the string.除非我从元组列表中传递数字,否则它不会对字符串进行切片。 and returns nothing in the last line.并在最后一行不返回任何内容。 Here is my stack trace or this code...这是我的堆栈跟踪或此代码...

line is ML_STARConfig, Master, C0RF, , rf, 7.1S B 2011-10-05, Firmware version
sfind list of characters is [0, 13, 21, 27, 29, 33, 52] and there should be a zero in position 0
line is ML_STARConfig, Master, C0RF, , rf, 7.1S B 2011-10-05, Firmware version
  word indexes are 52,33 and this should be a word =
line is ML_STARConfig, Master, C0RF, , rf, 7.1S B 2011-10-05, Firmware version
  word indexes are 33,29 and this should be a word =
line is ML_STARConfig, Master, C0RF, , rf, 7.1S B 2011-10-05, Firmware version
  word indexes are 29,27 and this should be a word =
line is ML_STARConfig, Master, C0RF, , rf, 7.1S B 2011-10-05, Firmware version
  word indexes are 27,21 and this should be a word =
line is ML_STARConfig, Master, C0RF, , rf, 7.1S B 2011-10-05, Firmware version
  word indexes are 21,13 and this should be a word =
line is ML_STARConfig, Master, C0RF, , rf, 7.1S B 2011-10-05, Firmware version
  word indexes are 13,0 and this should be a word =

the Variable for the line clearly works and the variable for the beginning and end indexes are there but the line[#:#] command just magically stops working and its very frustrating.该行的变量显然有效,并且开始和结束索引的变量在那里,但是 line[#:#] 命令只是神奇地停止工作并且非常令人沮丧。 I have no Idea what is causing this.我不知道是什么原因造成的。 I have tried making a list of lists of comma indexes and it is still not working.我已尝试制作逗号索引列表的列表,但它仍然无法正常工作。

What on earth am I missing here?我到底在这里想念什么?

I reversed the order of the slice and that fixed it.我颠倒了切片的顺序并修复了它。 Sorry I didnt realize I was trying to perform the slice in reverse, apparently this doesnt work.抱歉,我没有意识到我正在尝试反向执行切片,显然这不起作用。

to clarify, if you do澄清一下,如果你这样做

line[b:e]

and b>e it will give you a whole lot of nothing.而且b>e它会给你很多东西。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM