简体   繁体   English

使用python将字符追加到txt文件中的每一行

[英]Appending characters to each line in a txt file with python

I wrote the following python code snippet to append a lower p character to each line of a txt file: 我编写了以下python代码段,将小写p字符附加到txt文件的每一行:

f = open('helloworld.txt','r')
for line in f:
    line+='p'
print(f.read())
f.close()

However, when I execute this python program, it returns nothing but an empty blank: 但是,当我执行此python程序时,它只返回一个空空白:

zhiwei@zhiwei-Lenovo-Rescuer-15ISK:~/Documents/1001/ass5$ python3 helloworld.py

Can anyone tell me what's wrong with my codes? 谁能告诉我我的密码有什么问题吗?

Currently, you are only reading each line and not writing to the file. 当前,您只读取每一行,而不写入文件。 reopen the file in write mode and write your full string to it, like so: 在写入模式下重新打开文件并向其中写入完整的字符串,如下所示:

newf=""
with open('helloworld.txt','r') as f:
    for line in f:
        newf+=line.strip()+"p\n"
    f.close()
with open('helloworld.txt','w') as f:
    f.write(newf)
    f.close()

open(filePath, openMode) takes two arguments, the first one is the path to your file, the second one is the mode it will be opened it. open(filePath, openMode)有两个参数,第一个是文件的路径,第二个是将其打开的模式。 When you use 'r' as second argument, you are actually telling Python to open it as an only reading file. 当使用'r'作为第二个参数时,实际上是在告诉Python将其作为唯一读取文件打开。

If you want to write on it, you need to open it in writing mode, using 'w' as second argument. 如果要在其上进行写操作,则需要以写模式打开它,并使用'w'作为第二个参数。 You can find more about how to read/write files in Python in its official documentation . 您可以在其官方文档中找到有关如何在Python中读取/写入文件的更多信息。

If you want to read and write at the same time, you have to open the file in both reading and writing modes. 如果要同时读取和写入,则必须以读取和写入两种方式打开文件。 You can do this simply by using 'r+' mode. 您只需使用'r+'模式即可完成此操作。

well, type help(f) in shell, you can get "Character and line based layer over a BufferedIOBase object, buffer." 好了,在shell中键入help(f),您可以获得“ BufferedIOBase对象缓冲区上基于字符和行的图层”。 it's meaning:if you reading first buffer,you can get content, but again. 意思是:如果您读第一个缓冲区,您可以得到内容,但是再次获得。 it's empty。 so like this: 它是空的。所以像这样:

with open(oldfile, 'r') as f1, open(newfile, 'w') as f2:
       newline = ''
       for line in f1:
         newline+=line.strip()+"p\n"
         f2.write(newline)   

It seems that your for loop has already read the file to the end, so f.read() return empty string. 似乎您的for循环已经读取f.read()文件,因此f.read()返回空字符串。

If you just need to print the lines in the file, you could move the print into for loop just like print(line) . 如果只需要打印文件中的行,则可以像print(line)一样将打印内容移动到for循环中。 And it is better to move the f.read() before for loop: 并且最好将f.read()移至for循环之前:

f = open("filename", "r")
lines = f.readlines()
for line in lines:
    line += "p"
    print(line)
f.close()

If you need to modify the file, you need to create another file obj and open it in mode of "w", and use f.write(line) to write the modified lines into the new file. 如果需要修改文件,则需要创建另一个文件obj并以“ w”模式打开它,然后使用f.write(line)将修改后的行写入新文件。

Besides, it is more better to use with clause in python instead of open(), it is more pythonic. 此外,最好在python中使用with子句,而不要使用open(),它更像pythonic。

with open("filename", "r") as f:
    lines = f.readlines()
    for line in lines:
        line += "p"
        print(line)

When using with clause, you have no need to close file, this is more simple. 使用with子句时,您无需关闭文件,这更简单。

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

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