标签: substring

替换文件名的子串

抱歉,如果之前已经问过这个问题。我没有通过搜索找到答案。需要在 Python 中替换文件名的子字符串。

旧字符串:“_ready”

新字符串:“_busy”

文件:a_ready.txt、b_ready.txt、c.txt、d_blala.txt、e_ready.txt

输出:a_busy.txt、b_busy.txt、c.txt、d_blala.txt、e_busy.txt

有任何想法吗?我尝试使用 replce(),但没有任何反应。这些文件仍然使用旧名称。

这是我的代码:

import os

counter = 0

for file in os.listdir("c:\\test"):
    if file.endswith(".txt"):
        if file.find("_ready") > 0:
            counter = counter + 1
            print ("old name:" + file)
            file.replace("_ready", "_busy")
            print ("new name:" + file)
if counter == 0:
    print("No file has been found")
Run Code Online (Sandbox Code Playgroud)

python replace substring rename

0
推荐指数
1
解决办法
1万
查看次数

在没有内置函数的情况下在 Python 中查找字符串的子字符串

我正在尝试编写一些代码来查找字符串中的子字符串。到目前为止,我有这个:

main = "dedicated"
sub = "cat"
count = 0
for i in range (0,len(main)):
   match = True
   if sub[0]==main[i]:
     j=0
     for j in range(0,len(sub)):
         if sub[j]!=main[i+j]:
             match = False
             print "No substring"
             break
         else:
             count=count+1
             if match == True and count == len(sub):
                 print "Substring"
                 print "Position start:",i
Run Code Online (Sandbox Code Playgroud)
  • “奉献”和“猫”作品
  • “这是一个例子”和“例子”返回一个 IndexError
  • “无”和“不同”什么都不返回

任何人都可以帮助我/给我指点/改进代码,使其与上面的要点一起正常工作吗?

python string substring

0
推荐指数
1
解决办法
8563
查看次数

在C++中从C字符串中获取子字符串

我有 C 字符串

   char s[] = "n1=1&n2=2&name=test&sername=test2";
Run Code Online (Sandbox Code Playgroud)

我需要取字符串值名称,即“test”并写在一个单独的变量中。

所以我需要找到 "&name=" 和下一个 & 之间的值

c++ string substring

0
推荐指数
1
解决办法
4926
查看次数

Python 中 substr 和 strpos 的 PHP 等价物

我尝试将此php函数转换为Python

function trouveunebrique($contenu, $debut, $fin) {
  $debutpos = strpos($contenu, $debut);
  $finpos = strpos($contenu, $fin, $debutpos);
  if ($finpos == 0) {
    $finpos = strlen($contenu);
  }
  $nbdebut = strlen($debut);
  if ($debutpos > 0) {
    $trouveunebrique = substr($contenu, ($debutpos + $nbdebut), ($finpos - $debutpos - $nbdebut));
  } 
  else {
    $trouveunebrique = "";
  }

  return (trim($trouveunebrique));
}
Run Code Online (Sandbox Code Playgroud)

在这里搜索但找不到解决方案。我也试过这个:

   def trouveunebrique(contenu, debut, fin)
        debutpos = haystack.find(contenu, debut)
        finpos = haystack.find(contenu, fin)
        if (finpos == 0)
            finpos = …
Run Code Online (Sandbox Code Playgroud)

php python substring strpos python-2.7

0
推荐指数
1
解决办法
2017
查看次数

删除R中字符串的中间部分

如何去除刺的中间部分?例如,取字符串 - '2018_002.Feb'。对于此示例,我想删除“002.”,以便获得“2018_Feb”

谁能帮我?谢谢!

regex string substring r

0
推荐指数
1
解决办法
2862
查看次数

使用 gnu make 提取 6 个字符的子串

我有一个使用以下方法提取的 git 哈希字符串:

git describe --always --abbrev=6
Run Code Online (Sandbox Code Playgroud)

即我想得到 6 个字符。问题是如果 6 个字符的标签不是唯一的,git 似乎给了我 7 个字符。所以我想使用标准的 make / bash 命令(如 sed)提取前 6 个字符。make 本身似乎不支持子字符串。

目前我的 make 脚本包含以下内容:

foo:=$(lastword $(subst M,,$(subst :, ,$(shell git describe --always --abbrev=6))))
Run Code Online (Sandbox Code Playgroud)

这可能导致 foo=e94181c 但我喜欢它只是 e94181 以适应 24 位内存区域。

git makefile substring

0
推荐指数
1
解决办法
3230
查看次数

尝试提供非常长的字符串时,SubString给出OutOfMemory异常

我正在研究这段代码,以应对来自leet代码的编码挑战。

这段代码适用于987个测试案例中的986个。如果字符串很长(长度〜31600),则失败。substring函数将引发java.lang.OutOfMemoryError:Java堆空间。

我试图创建'substring'new SubString(s.substring(i,j)); 和s.substring(i,j).intern(); 在看到来自其他堆栈的建议之后。但徒劳

private static Map<String, Integer> findAllSubStrings(String s) {
        Map<String, Integer> allSubStrings = new HashMap<>();
        for(int i = 0; i<s.length(); i++) {
            for(int j=i+1; j<=s.length(); j++) {
                String substring = s.substring(i,j);
                allSubStrings.put(substring, substring.length());
            }
        }
        return allSubStrings;
    }
Run Code Online (Sandbox Code Playgroud)

例外是,

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.base/java.util.Arrays.copyOfRange(Arrays.java:4031)
at java.base/java.lang.StringLatin1.newString(StringLatin1.java:782)
at java.base/java.lang.String.substring(String.java:1888)
at main.LongestSubstring.findAllSubStrings(LongestSubstring.java:76)
at main.LongestSubstring.getLengthOfLongestSubString(LongestSubstring.java:44)
at main.LongestSubstring.main(LongestSubstring.java:38)
Run Code Online (Sandbox Code Playgroud)

我正在寻找可以帮助我了解和解决此问题的人。

提前致谢。

java string substring

0
推荐指数
1
解决办法
63
查看次数

在“” .join(e.0在element.split()中的e [0]的幕后发生了什么?

当我的Java经验有限时,我是一个尝试学习Python的初学者,所以请在这里忍受。

我可以区分代码各部分的目的。我知道我们实际上是将词组中每个单词的第一个字符连接到一个字符串中,因此实际上是首字母缩写词。我知道我们需要将短语解析为单词列表,并且对于每个单词,我们都可以弹出第一个字符,并且需要循环才能做到这一点。好吧,有道理。

但是到底呢?我们将for循环传递给join函数吗?当我在语法上不属于for循环的一部分时,我可以看到e [0]在这里如何发挥作用?

我认为语法可能使我陷入困境(对不起)。

python string split substring list

0
推荐指数
1
解决办法
26
查看次数

Python 检查字符串列表中的句子中是否存在字符串

我有一个单词列表substring = ["one","multiple words"],我想从中检查一个句子是否包含这些单词中的任何一个。

sentence1 = 'This Sentence has ONE word'
sentence2 = ' This sentence has Multiple Words'
Run Code Online (Sandbox Code Playgroud)

我使用任何运算符检查的代码:

any(sentence1.lower() in s for s in substring)
Run Code Online (Sandbox Code Playgroud)

即使这个词出现在我的句子中,这也是错误的。我不想使用正则表达式,因为这对于大量数据来说是一项昂贵的操作。

有没有其他方法可以解决这个问题?

python substring match python-3.7

0
推荐指数
1
解决办法
1851
查看次数

使用 Bash Perl 从命令输出中获取子字符串

让我们假设我正在处理的文本是(由 输出pecl install xdebug):

  |   - A list of all settings:  https://xdebug.org/docs-settings.php    |
  |   - A list of all functions: https://xdebug.org/docs-functions.php   |
  |   - Profiling instructions:  https://xdebug.org/docs-profiling2.php  |
  |   - Remote debugging:        https://xdebug.org/docs-debugger.php    |
  |                                                                      |
  |                                                                      |
  |   NOTE: Please disregard the message                                 |
  |       You should add "extension=xdebug.so" to php.ini                |
  |   that is emitted by the PECL installer. This does not work for      |
  |   Xdebug.                                                            |
  |                                                                      |
  +----------------------------------------------------------------------+


running: find "/tmp/pear/temp/pear-build-defaultuserNxuIJy/install-xdebug-2.9.2" | xargs …
Run Code Online (Sandbox Code Playgroud)

linux bash perl substring

0
推荐指数
1
解决办法
111
查看次数

标签 统计

substring ×10

python ×5

string ×5

bash ×1

c++ ×1

git ×1

java ×1

linux ×1

list ×1

makefile ×1

match ×1

perl ×1

php ×1

python-2.7 ×1

python-3.7 ×1

r ×1

regex ×1

rename ×1

replace ×1

split ×1

strpos ×1