Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.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 从.txt文件中的字符串更改特定行中的字符_Python_Python 2.7_Replace_File Io - Fatal编程技术网

Python 从.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

对于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].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'
。非常感谢!这正是我所需要的。我感谢你的帮助。