Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.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/8/file/3.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_File_Variables_Python 3.x - Fatal编程技术网

Python 如何更新文本文件中的变量

Python 如何更新文本文件中的变量,python,file,variables,python-3.x,Python,File,Variables,Python 3.x,我有一个开设账户的程序,有几行,但我想让它更新这一行credits=0每当购买时,我想让它在金额上再增加一行,这就是文件的样子 ['namef', 'namel', 'email', 'adress', 'city', 'state', 'zip', 'phone', 'phone 2'] credits = 0 这些信息保存在一个文本文件中,我不在乎你是否替换它(只要它还有1个),或者你是否只是更新它。请帮我解答:)抱歉,如果这个问题很简单,您可以根据包含要查找的键和相应值的字典创建一个通

我有一个开设账户的程序,有几行,但我想让它更新这一行
credits=0
每当购买时,我想让它在金额上再增加一行,这就是文件的样子

['namef', 'namel', 'email', 'adress', 'city', 'state', 'zip', 'phone', 'phone 2']

credits = 0

这些信息保存在一个文本文件中,我不在乎你是否替换它(只要它还有1个),或者你是否只是更新它。请帮我解答:)抱歉,如果这个问题很简单,您可以根据包含要查找的键和相应值的字典创建一个通用文本文件替换程序替换什么:

在模板文本文件中,将一些标志放在需要变量的位置:

['<namef>', 'namel', 'email', 'adress', 'city', 'state', 'zip', 'phone', 'phone 2']

credits = <credit_var>

下面的代码片段应该会让您了解如何进行。此代码更新文件counter_file.txt中计数器变量的值

import os

counter_file = open(r'./counter_file.txt', 'r+')
content_lines = []

for line in counter_file:
        if 'counter=' in line:
                line_components = line.split('=')
                int_value = int(line_components[1]) + 1
                line_components[1] = str(int_value)
                updated_line= "=".join(line_components)
                content_lines.append(updated_line)
        else:
                content_lines.append(line)

counter_file.seek(0)
counter_file.truncate()
counter_file.writelines(content_lines)
counter_file.close()

希望这能让你了解如何着手解决你的问题

如果有的话,你尝试过什么?你尝试过什么不起作用?你需要复制问题中的相关代码。您可能想更改保存格式,例如使用JSON(模块JSON)以简化数据处理我尝试了
文件中的行:if line==“credits”:a=line.split('=')b=int(a)writelines(b+1)
问题是我没有任何代码来做这件事,我不知道如何做
newfile = open('new_file.txt', 'w')
for l in open('template.txt'):
    for k,v in map_dict.iteritems():
        l = l.replace(k,str(v))
    newfile.write(l)
newfile.close()
import os

counter_file = open(r'./counter_file.txt', 'r+')
content_lines = []

for line in counter_file:
        if 'counter=' in line:
                line_components = line.split('=')
                int_value = int(line_components[1]) + 1
                line_components[1] = str(int_value)
                updated_line= "=".join(line_components)
                content_lines.append(updated_line)
        else:
                content_lines.append(line)

counter_file.seek(0)
counter_file.truncate()
counter_file.writelines(content_lines)
counter_file.close()