我最初尝试使用=
运算符来赋值但是它返回了一个错误,然后我尝试使用string.replace()
:
encrypted_str.replace(encrypted_str[j], dec_str2[k], 2)
Run Code Online (Sandbox Code Playgroud)
和
encrypted_str.replace(encrypted_str[j], unichr(ord(dec_str2[k]) - 32), 2)
Run Code Online (Sandbox Code Playgroud)
但它正在返回原始价值.
帮助了解如何正确使用替换API以提供正确的结果还有其他任何可用于代替的API unichr()
.
在encrypted_str
正在从用户采取的encrypted_str = raw_input()
dec_str2
是频率字符为通过用户输入.这个问题几乎不涉及我想知道的变量,如果我replcae()
错误地使用API,因为它给了我未更改的输出.encrypted_str
我们可以使用encrypted_str[j]
它将返回字符串中的字符来定义replace()
API 的子字符串.我使用encrypted_str.replace(encrypted_str[j], unichr(ord(dec_str2[k]) - 32), 1)
max replace 1而不是2(因为我只需要一个替换).
我需要完成的实际操作将在C中,如下所示:
encrypted_str[j] = dec_str2[k] -32
.
因为我是python的新手,所以我试图寻找替代品.
我想在包含网址的字符串中删除"http://",即:" http://www.google.com ".我的代码是:
import os
s = 'http://www.google.com'
s.replace("http://","")
print s
Run Code Online (Sandbox Code Playgroud)
我尝试用空格替换http://但不知何故它仍打印出http://www.google.com
我在这里使用不正确吗?感谢您的回答.
Stack Overflow 上有很多关于这个一般主题的问答,但它们要么质量很差(通常是初学者的调试问题暗示的),要么以其他方式错过了目标(通常是不够通用)。至少有两种极其常见的方法会使幼稚的代码出错,初学者从关于循环的规范中获益更多,而不是从将问题作为拼写错误或关于打印所需内容的规范中获益。所以这是我尝试将所有相关信息放在同一个地方。
假设我有一些简单的代码,可以对一个值进行计算x
并将其分配给y
:
y = x + 1
# Or it could be in a function:
def calc_y(an_x):
return an_x + 1
Run Code Online (Sandbox Code Playgroud)
现在我想重复计算 的许多可能值x
。我知道for
如果我已经有要使用的值列表(或其他序列),我可以使用循环:
xs = [1, 3, 5]
for x in xs:
y = x + 1
Run Code Online (Sandbox Code Playgroud)
while
或者,如果有其他逻辑来计算值序列,我可以使用循环x
:
def next_collatz(value):
if value % 2 == 0:
return value // 2
else:
return 3 * value + 1
def collatz_from_19():
x = 19
while x != 1:
x …
Run Code Online (Sandbox Code Playgroud) 我知道要删除单个反斜杠,我们可能会执行类似从Python 中的字符串中删除反斜杠的操作
我试图:
我想知道如何在下面的列表中删除所有像“\ue606”这样的词,
A =
['Historical Notes 1996',
'\ue606',
'The Future of farms 2012',
'\ch889',
'\8uuuu',]
Run Code Online (Sandbox Code Playgroud)
将其转化为
['Historical Notes 1996',
'The Future of farms 2012',]
Run Code Online (Sandbox Code Playgroud)
我试过:
A = ['Historical Notes 1996',
'\ue606',
'The Future of farms 2012',
'\ch889',
'\8uuuu',]
for y in A:
y.replace("\\", "")
A
Run Code Online (Sandbox Code Playgroud)
它返回:
['Historical Notes 1996',
'\ue606',
'The Future of farms 2012',
'\\ch889',
'\\8uuuu']
Run Code Online (Sandbox Code Playgroud)
我不确定如何处理 '\' 后面的字符串,或者为什么它添加了一个新的 '\' 而不是删除它。
Google maps API的标准返回值如下所示 - 从字节转换为字符串后:
b'{\n "destination_addresses" : [ "Washington, DC, USA" ],\n "origin_addresses" : [ "New York, NY, USA" ],\n "rows" : [\n {\n "elements" : [\n {\n "distance" : {\n "text" : "370 km",\n "value" : 369720\n },\n "duration" : {\n "text" : "3 hours 48 mins",\n "value" : 13672\n },\n "status" : "OK"\n }\n ]\n }\n ],\n "status" : "OK"\n}\n'
Run Code Online (Sandbox Code Playgroud)
为了进一步处理,我想首先删除所有换行符.但是,我不知道该怎么做
results_str.replace('\n', "")
Run Code Online (Sandbox Code Playgroud)
不起作用,.ie,它返回相同的字符串而不替换'\n',因为行继续符.
当我加倍反斜杠时也会发生同样的事情
results_str.replace('\\n', "")
Run Code Online (Sandbox Code Playgroud)
有小费吗?
如果python字符串是不可变的,如何更改如下:
a = "abc"
print a
a = "cde"
print a
Run Code Online (Sandbox Code Playgroud)
输出:
abc
cde
Run Code Online (Sandbox Code Playgroud)
这实际上是在创建一个新变量并改为指向它吗?
我有一个unicode文件路径列表,在其中我需要用英语变音符号替换所有变音符号。例如,我将ü与ue,ä与ae等。我定义了变音符号(键)及其变音符号(值)的字典。因此,我需要将每个密钥与每个文件路径进行比较,并在找到密钥的位置将其替换为值。这似乎很简单,但我无法使其正常工作。有没有人有任何想法?任何反馈,不胜感激!
到目前为止的代码:
# -*- coding: utf-8 -*-
import os
def GetFilepaths(directory):
"""
This function will generate all file names a directory tree using os.walk.
It returns a list of file paths.
"""
file_paths = []
for root, directories, files in os.walk(directory):
for filename in files:
filepath = os.path.join(root, filename)
file_paths.append(filepath)
return file_paths
# dictionary of umlaut unicode representations (keys) and their replacements (values)
umlautDictionary = {u'Ä': 'Ae',
u'Ö': 'Oe',
u'Ü': 'Ue',
u'ä': 'ae',
u'ö': 'oe',
u'ü': 'ue'
}
# get …
Run Code Online (Sandbox Code Playgroud) 我正在尝试找到一种非常简单的方法来删除字符串列表中的空格。因为我想改变列表年龄:
age = ['15', '23 years', '21']
Run Code Online (Sandbox Code Playgroud)
进入
age = ['15', '23years', '21']
Run Code Online (Sandbox Code Playgroud)
注意到“23”和“年”之间的空格是如何被删除的吗?我不确定为什么下面的 if 语句不起作用:
for x in age:
x.replace(" ", "")
Run Code Online (Sandbox Code Playgroud)
我在这里缺少什么?我后退一步,尝试从一个简单的字符串中删除空格:
test = 'hi hi hi'
Run Code Online (Sandbox Code Playgroud)
以下代码可用于删除空格:
test.replace(" ", "")
Run Code Online (Sandbox Code Playgroud)
当调用 'test 时返回此值:
'hihihi'
Run Code Online (Sandbox Code Playgroud)
那么为什么我不能像上面那样添加一个 for 循环来迭代字符串列表并删除空格呢?什么代码可以让我解决我的问题?
感谢您的帮助!
我正在尝试替换字符串中的某些内容.这是我正在测试的代码:
stringT = "hello world"
print(stringT)
stringT.replace("world", "all")
print(stringT)
Run Code Online (Sandbox Code Playgroud)
我希望第二个输出能说"你好所有",但两次都说"你好世界".没有错误,代码运行正常,它只是没有做任何事情.我该如何解决?
我有一个.txt
文件表单,我读取多行,并在每行附加一个数组.不幸的是,我也有阵列中的换行符.
当我尝试更换它们时line.replace("\n", "")
,什么都不会发生.
python ×10
string ×5
replace ×2
for-loop ×1
iteration ×1
line-breaks ×1
regex ×1
unicode ×1
whitespace ×1