我有一根长长的绳子:
string = 'Idnum\rId\nkey: maturity\n2\nmaturity\rpara1\rpara2\n1Y\r0\r0\n2Y\r0\r0'
现在我想找出单词\n
前面的\nmaturity
的数字(在字符串中是唯一的,我们可以看到答案是2)
这是这个问题的动机,如果你不能理解我下面说的话,就忽略它,把注意力集中在上面的问题上
我想使用pandas
来读取data
(我不确定data
的结构,可能是csv):
并使用以下语法:
value = pandas.read_csv(data, sep = '\t', skiprows = 3).set_index('maturity')
所以我需要找出maturity
的行号来决定我应该跳过多少行。但我只知道data
data.getvalue() = 'Idnum\rId\nkey: maturity\n2\nmaturity\rpara1\rpara2\n1Y\r0\r0\n2Y\r0\r0'
(\n
正在更改行,\r
正在更改单元格)。因此,我必须天真地在maturity
之前找到\n
的数目,以确定原始表中maturity
的行数(或data
)
所以,如果你熟悉上面的结构,你能告诉我如何在data
中找到maturity
的行号吗
一种方法是:
split
以'\nmaturity'作为分隔符的字符串,并取第一个(因此位于找到的第一个项的左侧)李>count
来计算'\n'的数目李>未经测试:
相关问题 更多 >
编程相关推荐