Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 替换从某个位置开始的字符串的一部分_Python_String_Python 2.x - Fatal编程技术网

Python 替换从某个位置开始的字符串的一部分

Python 替换从某个位置开始的字符串的一部分,python,string,python-2.x,Python,String,Python 2.x,我试图有条件地更新从指定位置开始的字符串的一部分。我有这样的想法: i = 0 bre = 'newtext' with open(myfile, "r") as f: data = f.readlines() for line in data: if i > 0 and line[98] == '1': print 'ok' line[1562] = bre i += 1 #

我试图有条件地更新从指定位置开始的字符串的一部分。我有这样的想法:

i = 0
bre = 'newtext'
with open(myfile, "r") as f:
    data = f.readlines()
    for line in data:
        if i > 0 and line[98] == '1':
            print 'ok'
            line[1562] = bre
        i += 1
        # write line to a file
我收到的错误是:

Traceback (most recent call last):
  File "test.py", line 19, in <module>
    line[1562] = bre

您正在尝试将字符串中的一个字符元素更改为另一个字符串

line = line[:1562] + bre + line[1563:]

# to skip the length of your bre
line = line[:1562] + bre + line[(1562+len(bre)):]
范例

bre = 'newtext'

myString = "asdfghjkl"

#replace character at index 2 with my string bre
myString = myString [:2]+ bre+ myString [3:]

print(myString)
s = "abc"
a[1] = 'z' # is wrong because 'str' object does not support item assignment

s = a[:1] + 'z' + a[2:] #but this will work
# this takes the pointer s and points it a completely new string
字符串也不像列表一样是可变的

无法转到索引并更改字符

范例

bre = 'newtext'

myString = "asdfghjkl"

#replace character at index 2 with my string bre
myString = myString [:2]+ bre+ myString [3:]

print(myString)
s = "abc"
a[1] = 'z' # is wrong because 'str' object does not support item assignment

s = a[:1] + 'z' + a[2:] #but this will work
# this takes the pointer s and points it a completely new string
有趣的事实:
这就是为什么可以将字符串用作字典中的键,而不是列表中的键。字符串可以散列

您试图将字符串中的一个字符元素更改为另一个字符串

line = line[:1562] + bre + line[1563:]

# to skip the length of your bre
line = line[:1562] + bre + line[(1562+len(bre)):]
范例

bre = 'newtext'

myString = "asdfghjkl"

#replace character at index 2 with my string bre
myString = myString [:2]+ bre+ myString [3:]

print(myString)
s = "abc"
a[1] = 'z' # is wrong because 'str' object does not support item assignment

s = a[:1] + 'z' + a[2:] #but this will work
# this takes the pointer s and points it a completely new string
字符串也不像列表一样是可变的

无法转到索引并更改字符

范例

bre = 'newtext'

myString = "asdfghjkl"

#replace character at index 2 with my string bre
myString = myString [:2]+ bre+ myString [3:]

print(myString)
s = "abc"
a[1] = 'z' # is wrong because 'str' object does not support item assignment

s = a[:1] + 'z' + a[2:] #but this will work
# this takes the pointer s and points it a completely new string
有趣的事实:
这就是为什么可以将字符串用作字典中的键,而不是列表中的键。字符串可以散列

在python中,字符串是不可变的。此外,当您执行line[1562]=bre时,您正在尝试将stringbre分配给另一行中的字符行[1562]。幸运的是,用python解决这个问题非常简单。一种很好的方法是将所有前一行字符串与bre和后一行字符串连接起来。最后,您可以将其分配给var行。 差不多

line = line[:1562] + bre + line[1563:]
i = 0
bre = 'newtext'
with open(myfile, "r") as f:
    data = f.readlines()
for x,line in enumerate(data):
    if i > 0 and line[98] == '1':
        print 'ok'
        data[x] = line[:1562] + bre + line[1563:]
    i += 1

with open(new_file, 'w') as f
    for line in data:
      f.write(data)
但请注意,这样做时,数据对象内的行不会改变。你只是换了一份而已。如果你想读一个文件行,然后把它写在文件中,你需要

line = line[:1562] + bre + line[1563:]
i = 0
bre = 'newtext'
with open(myfile, "r") as f:
    data = f.readlines()
for x,line in enumerate(data):
    if i > 0 and line[98] == '1':
        print 'ok'
        data[x] = line[:1562] + bre + line[1563:]
    i += 1

with open(new_file, 'w') as f
    for line in data:
      f.write(data)

在python中,字符串是不可变的。此外,当您执行line[1562]=bre时,您正在尝试将stringbre分配给另一行中的字符行[1562]。幸运的是,用python解决这个问题非常简单。一种很好的方法是将所有前一行字符串与bre和后一行字符串连接起来。最后,您可以将其分配给var行。 差不多

line = line[:1562] + bre + line[1563:]
i = 0
bre = 'newtext'
with open(myfile, "r") as f:
    data = f.readlines()
for x,line in enumerate(data):
    if i > 0 and line[98] == '1':
        print 'ok'
        data[x] = line[:1562] + bre + line[1563:]
    i += 1

with open(new_file, 'w') as f
    for line in data:
      f.write(data)
但请注意,这样做时,数据对象内的行不会改变。你只是换了一份而已。如果你想读一个文件行,然后把它写在文件中,你需要

line = line[:1562] + bre + line[1563:]
i = 0
bre = 'newtext'
with open(myfile, "r") as f:
    data = f.readlines()
for x,line in enumerate(data):
    if i > 0 and line[98] == '1':
        print 'ok'
        data[x] = line[:1562] + bre + line[1563:]
    i += 1

with open(new_file, 'w') as f
    for line in data:
      f.write(data)

您在问题中提供的上下文太少,无法给出完整的答案,但我发现您的代码存在一个主要问题:您试图分配给str对象,但str是不可变的

如果您报告了完整的回溯,您会注意到如下情况:

TypeError:“str”对象不支持项分配

因此,您必须从现有字符串开始创建新字符串,例如:

s = 'I like dogs more than mice!'
t = 'cat'
n = 7
u = s[:n] + t + s[n + len(t):]
print(u)
# I like cats more than mice!

您在问题中提供的上下文太少,无法给出完整的答案,但我发现您的代码存在一个主要问题:您试图分配给str对象,但str是不可变的

如果您报告了完整的回溯,您会注意到如下情况:

TypeError:“str”对象不支持项分配

因此,您必须从现有字符串开始创建新字符串,例如:

s = 'I like dogs more than mice!'
t = 'cat'
n = 7
u = s[:n] + t + s[n + len(t):]
print(u)
# I like cats more than mice!

问题是什么?@norok2-编辑以反映错误消息。问题是什么?@norok2-编辑以反映错误消息。感谢您的回复。这几乎就是我正在寻找的行为,但是,我希望新的字符串取代这10个位置。通过添加myString,它增加了字符串的长度。字符串本身是一条固定宽度的线。我很高兴它有帮助,请检查我是否添加了更新。您可能还应该报告为什么s='hello';s[3]=“世界”不起作用。从你的短信来看,虽然上面的这些都不起作用,但像s='hello';s[3]=“x”会,但它不会。感谢您的洞察力,我在我的回复中添加了这一点:感谢您的回复Kuldeep。这几乎就是我正在寻找的行为,但是,我希望新的字符串取代这10个位置。通过添加myString,它增加了字符串的长度。字符串本身是一条固定宽度的线。我很高兴它有帮助,请检查我是否添加了更新。您可能还应该报告为什么s='hello';s[3]=“世界”不起作用。从你的短信来看,虽然上面的这些都不起作用,但像s='hello';s[3]=“x”会,但它不会。感谢您的洞察力,我在我的回答中添加了这一点:第一条语句不是真的,字符串在Python中是不可变的,列表不是!第一条语句不是真的,字符串在Python中是不可变的,列表不是!