抱歉,如果之前已经问过这个问题。我没有通过搜索找到答案。需要在 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) 我正在尝试编写一些代码来查找字符串中的子字符串。到目前为止,我有这个:
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任何人都可以帮助我/给我指点/改进代码,使其与上面的要点一起正常工作吗?
我有 C 字符串
char s[] = "n1=1&n2=2&name=test&sername=test2";
Run Code Online (Sandbox Code Playgroud)
我需要取字符串值名称,即“test”并写在一个单独的变量中。
所以我需要找到 "&name=" 和下一个 & 之间的值
我尝试将此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) 如何去除刺的中间部分?例如,取字符串 - '2018_002.Feb'。对于此示例,我想删除“002.”,以便获得“2018_Feb”
谁能帮我?谢谢!
我有一个使用以下方法提取的 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 位内存区域。
我正在研究这段代码,以应对来自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经验有限时,我是一个尝试学习Python的初学者,所以请在这里忍受。
我可以区分代码各部分的目的。我知道我们实际上是将词组中每个单词的第一个字符连接到一个字符串中,因此实际上是首字母缩写词。我知道我们需要将短语解析为单词列表,并且对于每个单词,我们都可以弹出第一个字符,并且需要循环才能做到这一点。好吧,有道理。
但是到底呢?我们将for循环传递给join函数吗?当我在语法上不属于for循环的一部分时,我可以看到e [0]在这里如何发挥作用?
我认为语法可能使我陷入困境(对不起)。
我有一个单词列表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)
即使这个词出现在我的句子中,这也是错误的。我不想使用正则表达式,因为这对于大量数据来说是一项昂贵的操作。
有没有其他方法可以解决这个问题?
让我们假设我正在处理的文本是(由 输出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)