简体   繁体   English

将新字符串添加到文本文件中特定行的末尾

[英]Adding a new string to the end of a specific line in a text file

I'm new to python hence I am unable to implement the solutions I've found online in order to fix my problem. 我是python的新手,因此我无法实施我在网上找到的解决方案来解决问题。 I am trying to add a specific string to the end of a specific line to a textfile. 我正在尝试将特定字符串添加到文本文件的特定行的末尾。 As I understand text commands, I must overwrite the file if I don't want to append to the end of it. 据我了解的文本命令,如果我不想附加到文件末尾,则必须覆盖该文件。 So, my solution is as follows: 因此,我的解决方案如下:

    ans = 'test'
numdef = ['H',2] 
f = open(textfile, 'r')
lines = f.readlines()
f.close()
f = open(textfile, 'w')
f.write('')
f.close()
f = open(textfile, 'a')
for line in lines:
    if int(line[0]) == numdef[1]:
        if str(line[2]) == numdef[0]:
                k = ans+ line
                f.write(k)
    else:
        f.write(line)

Basically, I am trying to add variable ans to the end of a specific line, the line which appears in my list numdef . 基本上,我试图将变量ans添加到特定行的末尾,该行出现在我的列表numdef So, for example, for 因此,例如,

2 H: 4,0 : Where to search for information : google 2小时:4,0:在哪里搜索信息:谷歌

I want 我想要

2 H: 4,0 : Where to search for information : google test 2小时:4,0:在哪里搜索信息:google测试

I have also tried using line.insert() but to no avail. 我也试过使用line.insert()但无济于事。

I understand using the 'a' function of the open command is not so relevant and helpful here, but I am out of ideas. 我知道使用open命令的'a'功能在这里不是很相关且没有帮助,但是我没有主意。 Would love tips with this code, or if maybe I should scrap it and rethink the whole thing. 可能会喜欢此代码的提示,或者如果我应该废弃它并重新考虑整个问题。 Thank you for your time and advice! 感谢您的时间和建议!

Try this. 尝试这个。 You don't have an else case if it meets the first requirement but not the other. 如果满足第一个要求,但没有另一个条件,则没有其他情况。

ans = 'test'
numdef = ['H',2] 
f = open(textfile, 'r')
lines = f.readlines()
f.close()
f = open(textfile, 'w')
f.write('')
f.close()
f = open(textfile, 'a')
for line in lines:
    if int(line[0]) == numdef[1] and str(line[2]) == numdef[0]:
        k = line.replace('\n','')+ans
        f.write(k)
    else:
        f.write(line)
f.close()

Better way: 更好的方法:

#initialize variables
ans = 'test' 
numdef = ['H',2]  
#open file in read mode, add lines into lines
with open(textfile, 'r') as f:
    lines=f.readlines() 
#open file in write mode, override everything    
with open(textfile, 'w') as f: 
    #in the list comprehension, loop through each line in lines, if both of the conditions are true, then take the line, remove all newlines, and add ans. Otherwise, remove all the newlines and don't add anything. Then combine the list into a string with newlines as separators ('\n'.join), and write this string to the file.
    f.write('\n'.join([line.replace('\n','')+ans if int(line[0]) == numdef[1] and str(line[2]) == numdef[0] else line.replace('\n','') for line in lines]))

When you use the method 使用方法时

lines = f.readlines()

Python automatically adds "\\n" to the end of each line. Python自动在每行末尾添加“ \\ n”。

Try instead of : 尝试代替:

k = line+ans k =线+ ans

The following: 下列:

k = line.rstrip('\n') + ans

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM