【问题标题】:Python does not write to a file when running in a loopPython 在循环中运行时不会写入文件
【发布时间】:2020-08-02 19:10:25
【问题描述】:

我有一个 Python 文件 file1.py,其中包含以下代码行:

uname="auto1@mailnesia.com"
pwd="abcdef"

我想将uname替换并更改为“auto2mailnesia.com”,然后更改为“auto3@mailnesia.com”等等,然后使用在另一个文件中更新了uname。 考虑一个场景,我想将 unameauto1 更新为 auto3

我有另一个 Python 文件 file2.py,其代码行如下:

from xxx.xxx import file1

for i in range(0,1):
    currentuser = (uname[uname.index('auto') + 4:uname.index('@')])
    newuser = str(int(currentuser) + 1)
    newusername = uname.replace(currentuser, newuser)
    print(uname)
    print(currentuser)
    print(newusername)
    print(newuser)

    with open(testdataFileName, 'r+') as f:
        text = f.read()
        text = re.sub(uname, newusername, text)
        f.seek(0)
        f.write(text)

当我在 1 的循环中运行 file2.py 时,uname 会在 file1.py 中正确更新

输出如下:

auto1@mailnesia.com
1
auto2@mailnesia.com
2

但是,当我在循环 3 中运行 file2.py 时,uname 只会在 file1.py 中更新一次。

输出如下:

test_auto1@mailnesia.com
1
test_auto2@mailnesia.com
2
test_auto1@mailnesia.com
1
test_auto2@mailnesia.com
2
test_auto1@mailnesia.com
1
test_auto2@mailnesia.com
2

我不明白为什么uname 在循环运行时只在file1.py 中更新一次。

有人可以解释一下吗?

另外,如果有人能告诉我我做错了什么以及如何解决这个问题,我将不胜感激。

【问题讨论】:

  • 这能回答你的问题吗? Writing to a file in a for loop
  • 你实际上print的价值观是什么?现在,看看代码。为什么这些值应该由文件内容更新?您更改了文件的内容,但这有什么关系呢?
  • 无论如何,如果您想验证文件内容是否已更改,最准确的方法是实际检查文件,例如在文本编辑器中打开它。
  • 如果我理解正确,你想运行 file2.py,它会读取 file1.py。然后将file1.py(其中uname="auto1@mailnesia.com")中的值转换为uname="auto2@mailnesia.com"。如果您多次运行它,我假设您希望它创建新的 uname 和 pwd 对组,其中用户名不断从 1 增加到 2 到 3 到 4 到 .... 您运行的次数。我的理解正确吗?
  • 是的;您将其存储在text 中。下次通过for 循环会发生什么?发生的第一件事是currentuser = (uname[uname.index('auto') + 4:uname.index('@')])。但是uname 现在与之前的值相同,因此currentuser 将获得与上次相同的值,依此类推。当您这次打开文件时,您将再次阅读文本,但随后尝试将 same uname 替换为 same newusername。这就是uname 只更新一次的原因,正如您所抱怨的那样:因为循环中的任何内容都不会更改 uname 的值。

标签: python for-loop file-handling


【解决方案1】:

您的问题是 uname 在每次迭代中都保持不变(无论 i 是什么)。它不会在循环的每次迭代结束时更新。相反,我建议保留变量currentusernamecurrentuser,以便可以保留最后一个用户名和索引(或ID)并因此可以访问。这些将在循环开始之前初始化,并在每次下一次迭代开始之前更新。

import re
uname="auto1@mailnesia.com"
pwd="abcdef"

# initialize
currentusername = uname
currentuser = (uname[uname.index('auto') + 4:uname.index('@')])

for i in range(0,4):
    print('\niteration ' + str(i))
    newuser = str(int(currentuser) + 1)
    newusername = currentusername.replace(currentuser, newuser)
    print("uname=" + uname)
    print("currentuser=" + currentuser)
    print("newusername=" + newusername)
    print("newuser=" + newuser)

    with open('somefile.dat', 'r+') as f:
        text = f.read()
        text = re.sub(currentusername, newusername, text)
        f.seek(0)
        f.write(text)

    # update for next iteration
    currentuser = newuser
    currentusername = newusername

输出:

iteration 0
currentusername=auto1@mailnesia.com
currentuser=1
newusername=auto2@mailnesia.com
newuser=2

iteration 1
currentusername=auto2@mailnesia.com
currentuser=2
newusername=auto3@mailnesia.com
newuser=3

iteration 2
currentusername=auto3@mailnesia.com
currentuser=3
newusername=auto4@mailnesia.com
newuser=4

iteration 3
currentusername=auto4@mailnesia.com
currentuser=4
newusername=auto5@mailnesia.com
newuser=5

最后,somefile.dat 将包括auto5@mailnesia.com

【讨论】:

  • 我正在更改 uname 的值并将新更改的值存储在 newusername 然后我用下面的行将 file1 中的 uname 替换为 newusername ` with open(testdataFileName, 'r+') as f: text = f.read() text = re.sub(uname, newusername, text) f.seek(0) f.write(text) `
  • 我运行了你的代码并得到了以下输出iteration 0 uname=test_auto1@mailnesia.com currentuser=1 newusername=test_auto2@mailnesia.com newuser=2 iteration 1 uname=test_auto1@mailnesia.com currentuser=2 newusername=test_auto3@mailnesia.com newuser=3 iteration 2 uname=test_auto1@mailnesia.com currentuser=3 newusername=test_auto4@mailnesia.com newuser=4正如你在上面看到的,每次迭代中的uname都是一样的,那么currentuser每次如何变化,因为据我所知, currentuser 是从 uname 计算出来的
  • 原来是从uname计算出来的,后来在靠近底部的循环中更新了。希望这是有道理的。
【解决方案2】:

我知道你已经有很多答案了。我想我会稍微简化你的代码。您只需阅读第一行。

with open('your_filename.txt', 'r+') as f: #update your_filename with your filename
    text = f.readline()
    print ('previous value in file :', text)
    cuser = int(text[11:text.index('@')]) + 1
    text = text[:11]+str(cuser)+text[text.index('@'):]
    f.seek(0)
    f.write(text)
    print ('new value in file : ', text)

每次运行时的输出都会发生变化。我的第一次跑步给了我:

previous value in file : uname="auto1@mailnesia.com"

new value in file :  uname="auto2@mailnesia.com"

文件内容显示为:

uname="auto2@mailnesia.com"
pwd="abcdef"

我的第四次跑步给了我:

previous value in file : uname="auto3@mailnesia.com"

new value in file :  uname="auto4@mailnesia.com"

文件内容显示为:

uname="auto4@mailnesia.com"
pwd="abcdef"

如您所见,每次我运行代码时,文件都会不断更新。我不知道你为什么需要循环几次。您是否尝试在每次运行时多次更改文件中的值?

我不确定您是否真的需要循环。我更新了我的代码以创建一个循环,它仍然有效。这是更新的代码。我只是用了一个简单的for loop

for i in range(4):

    with open('xyz.txt', 'r+') as f: #update your_filename with your filename

        text = f.readline()

        print ('previous value in file :', text)

        cuser = int(text[11:text.index('@')]) + 1
        text = text[:11]+str(cuser)+text[text.index('@'):]

        f.seek(0)
        f.write(text)

        print ('new value in file : ', text)

这是我得到的输出:

previous value in file : uname="auto4@mailnesia.com"

new value in file :  uname="auto5@mailnesia.com"

previous value in file : uname="auto5@mailnesia.com"

new value in file :  uname="auto6@mailnesia.com"

previous value in file : uname="auto6@mailnesia.com"

new value in file :  uname="auto7@mailnesia.com"

previous value in file : uname="auto7@mailnesia.com"

new value in file :  uname="auto8@mailnesia.com"

文件中的值为:

uname="auto8@mailnesia.com"
pwd="abcdef"

【讨论】:

  • 感谢您的详细解释您的代码运行良好我正在循环运行它,因为我需要测试假设 10 个用户的登录,从 auto1 到 auto 10(将​​来可以继续到 100或 1000 个用户)并且我不想在 file1.py 中存储 10 个不同的 uname 我正在从 file1.py 读取 uname,然后将其作为参数传递给登录函数您的代码将从 file1.py 读取并更改第一行假设我在 line1 上有 uname,在 line2 上有 pswd,在 line3 上有电话号码,在第 4 行有员工 ID,我只想更改 line1、3、4,而不是 pswd。如何做到这一点
  • 在这种情况下,您将不得不循环读取文件。并更改每一行的信息。 for f1 in f: 将帮助您阅读每一行。您可以检查每一行的内容并进行更改。
  • 如果它解决了您的问题,您可以将我的回复标记为已回答。另外,如果你想用新的源文件更新你的问题,我可以用代码编辑响应来解决它。
猜你喜欢
  • 2015-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-14
  • 2020-10-05
  • 2019-08-24
  • 2018-01-12
相关资源
最近更新 更多