简体   繁体   English

查找长字符串中给定单词之前的 \\n 个数

[英]Find the number of \n before a given word in a long string

I have a long string:我有一个很长的字符串:

string = 'Idnum\rId\nkey: maturity\n2\nmaturity\rpara1\rpara2\n1Y\r0\r0\n2Y\r0\r0'

Now I want to find out the number of \\n before the word \\nmaturity (is unique in the string and we can see the answer is 2).现在我想找出单词\\nmaturity之前的\\n数量(在字符串中是唯一的,我们可以看到答案是 2)。

Here is the motivation of this question, if you cannot understand what I say below, just ignore it and focus on the question above.这是这个问题的动机,如果你不能理解我下面说的内容,请忽略它并专注于上面的问题。

I want to use pandas to read the data (I am not sure for the structure of data , may be a csv):我想使用pandas来读取data (我不确定data的结构,可能是一个csv):

在此处输入图片说明

and use the syntax:并使用语法:

value = pandas.read_csv(data, sep = '\t', skiprows = 3).set_index('maturity') 

to skip first 3 rows before maturity to obtain the table like:maturity前跳过前3行以获得如下表: 在此处输入图片说明

So I need to find out the row number of maturity to determine how many rows I should skip.所以我需要找出maturity的行数来确定我应该跳过多少行。 But what I only know for data is但我只知道data

data.getvalue() = 'Idnum\rId\nkey: maturity\n2\nmaturity\rpara1\rpara2\n1Y\r0\r0\n2Y\r0\r0'

( \\n is changing the line and \\r is changing the cell). \\n正在更改行,而\\r正在更改单元格)。 So I have to naively find the number of \\n before maturity to determine the row number of maturity in the original table (or data ).所以我必须天真地找到maturity前的\\n数来确定原始表(或data )中maturity的行数。

So, if you are familiar with above structure, could you tell me how to find the row number of maturity in the data ?那么,如果您熟悉上述结构,您能告诉我如何在data找到maturity的行数吗?

One way to do it:一种方法:

  • First split the string with '\\nmaturity' as separator and take the first (thus left of the first found item).首先使用 '\\nmaturity' 作为分隔符split字符串并取第一个(因此位于第一个找到的项目的左侧)。
  • Than use count to count the number of '\\n's.比使用count来计算 '\\n' 的数量。

Untested:未经测试:

string = 'Idnum\rId\nkey: maturity\n2\nmaturity\rpara1\rpara2\n1Y\r0\r0\n2Y\r0\r0'
stringUntilMaturity = string.split('\nmaturity')[0]
counts = stringUntilMaturity.count('\n')

暂无
暂无

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

相关问题 如何找到一个单词在给定字符串中连续重复的最大次数? - How to find the largest number of times a word is repeated consecutively in a given string? 查找出现在字符串中单词之前的子字符串直到一个数字 - Find a substring that appears before a word in a string upto a number 我想在Python 2.7中的长字符串(段落)中提取给定单词周围的一定数量的单词 - I want to extract a certain number of words surrounding a given word in a long string(paragraph) in Python 2.7 如何使用正则表达式在数字之前找到以“n't”结尾的单词? - How can I find a word ending with "n't" before a number using Regex? 在已知字符串中出现N次的情况下,查找在字符串中重复哪个术语 - Find which term is repeated in a string, when given a known number of N occurrences in the string 给定一个长度为 n 的字符串数组,找出相似的字符串对的数量 - given an array of strings words of length n, find the number of pairs of string that are similar 查找给定字符串是否是python中的单词,字符或数字 - finding if a given string is a word, a character or a number in python 给定一个长字符串,在字典中找到匹配的字符串 - Given a long string, find the matching strings in the dictionary 正则表达式在数字之后或之前立即查找单词 - Regex to find immediately a word after or before the a number 查找给定字符串中特定单词的频率 - Find frequency for a specific word in a given string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM