Python 从.txt文件中的字符串更改特定行中的字符
对于Python 2.7.13: 我试图读取.txt文件中的行,并从特定行中更改一个字符 线条本身看起来像这样:Python 从.txt文件中的字符串更改特定行中的字符,python,python-2.7,replace,file-io,Python,Python 2.7,Replace,File Io,对于Python 2.7.13: 我试图读取.txt文件中的行,并从特定行中更改一个字符 线条本身看起来像这样: with open('data.txt', 'r') as file: data = file.readlines() #This is the second line of the .txt file and the chr is in the position 48 of the string print data[1][48] string = data[1][48
with open('data.txt', 'r') as file:
data = file.readlines()
#This is the second line of the .txt file and the chr is in the position 48 of the string
print data[1][48]
string = data[1][48].replace('N','Y')
data[1] = string
with open('data.txt', 'w') as file:
file.writelines( data )
程序
我想转到字符串的最后一个字符(在本例中为N),并在同一个.txt文件中将其优先更改为“Y”(如果可能,不需要创建其他文件)
到目前为止,我的代码如下所示:
with open('data.txt', 'r') as file:
data = file.readlines()
#This is the second line of the .txt file and the chr is in the position 48 of the string
print data[1][48]
string = data[1][48].replace('N','Y')
data[1] = string
with open('data.txt', 'w') as file:
file.writelines( data )
你能帮帮我吗
with open('data.txt', 'r') as file:
data = file.readlines()
LINE = 0
COLUMN = 2
CHARACTER = 'Z'
d = list(data[LINE])
d[COLUMN] = CHARACTER
data[LINE] = "".join(d)
with open('data.txt', 'w') as file:
file.writelines(data)
这应该对你有用
这应该能帮到你。
LINE=1
&COLUMN=48
和CHARACTER='Y'
,谢谢!这正是我所需要的。非常感谢您的帮助。LINE=1
&COLUMN=48
和CHARACTER='Y'
。非常感谢!这正是我所需要的。我感谢你的帮助。