简体   繁体   English

为什么我不能在 Python 中将列表的第一个元素写入文本文件?

[英]Why I can´t write the first element of a list into a text file in Python?

I got a text file like this我有一个这样的文本文件

Bruce
brucechungulloa@outlook.com

I've used this to read the text file and export it to a list我用它来读取文本文件并将其导出到列表

with open('info.txt') as f:
    info =  f.readlines()            
    for item in info:
        reportePaises = open('reportePaises.txt', 'w')
        reportePaises.write("%s\n" % item)

But when I want to write the elements of the list(info) into another text file, only the info[1] is written (the mail)但是当我想将列表(信息)的元素写入另一个文本文件时,只写入信息[1](邮件)

How can I write the entire list onto the text file?如何将整个列表写入文本文件?

with open('data.csv') as f:
    with open('test2.txt', 'a') as wp:
        for item in f.readlines():
            wp.write("%s" % item)
        wp.write('\n') # adds a new line after the looping is done

That will give you:这会给你:

Bruce布鲁斯

brucechungulloa@outlook.com brucechungulloa@outlook.com

In both files.在这两个文件中。

You were having problems because every time you open a file with 'w' flag, you overwrite it on the disk.您遇到了问题,因为每次打开带有'w'标志的文件时,都会在磁盘上覆盖它。 So, you created a new file every time.因此,您每次都创建了一个新文件。

You should open the second file only once, in the with statement:您应该在 with 语句中只打开第二个文件一次:

with open('info.txt') as f, open('reportePaises.txt', 'w') as reportePaises:
    info =  f.readlines()            
    for item in info:
        reportePaises.write(item)

As @Pynchia suggested, it's probably better not to use .readlines() , and loop directly on input file instead.正如@Pynchia 建议的那样,最好不要使用.readlines() ,而是直接在输入文件上循环。

with open('info.txt') as f, open('reportePaises.txt', 'w') as reportePaises:          
    for item in f:
        reportePaises.write(item)

This way you don't create a copy of the while file in your RAM by saving it to a list, which may cause a huge delay if the file is big (and, obviously, uses more RAM).通过这种方式,您不会通过将文件保存到列表来在 RAM 中创建 while 文件的副本,如果文件很大(并且显然使用更多 RAM),这可能会导致巨大的延迟。 Instead, you treat the input file as an iterator and just read next line directly from your HDD on each iteration.相反,您将输入文件视为迭代器,并在每次迭代时直接从 HDD 读取下一行。

You also (if I did the testing right) don't need to append '\\n' to every line.你也(如果我做的测试正确)不需要在每一行附加'\\n' The newlines are already in item .换行符已经在item Because of that you don't need to use string formatting at all, just reportePaises.write(item) .因此,您根本不需要使用字符串格式,只需reportePaises.write(item)

You are opening your file in write mode every time you write to a file, effectively overwriting the previous line that you wrote.每次写入文件时,您都以写入模式打开文件,从而有效地覆盖了您写入的前一行。 Use the append mode, a , instead.使用附加模式a代替。

reportePaises = open('reportePaises.txt', 'a')

Edit: Alternatively, you can open the file once and instead of looping through the lines, write the whole contents as follows:编辑:或者,您可以打开文件一次,而不是遍历行,按如下方式编写整个内容:

with open('reportePaises.txt', 'w') as file:
    file.write(f.read())

Try this without open output file again and again.在不打开输出文件的情况下一次又一次地尝试此操作。

with open('info.txt') as f:
    info =  f.readlines()            

with open('reportePaises.txt', 'w') as f1:
    for x in info:
        f1.write("%s\n" % x)

That will work.那可行。

Two problems here.这里有两个问题。 One is you are opening the output file inside the loop.一种是您在循环内打开输出文件。 That means it is being opened several times.这意味着它被多次打开。 Since you also use the "w" flag that means the file is truncated to zero each time it is opened.由于您还使用了“w”标志,这意味着每次打开文件时都会将其截断为零。 Therefore you only get the last line written.因此,您只能写入最后一行。

It would be better to open the output file once outside the loop.最好在循环外打开输出文件。 You could even use an outer with block.您甚至可以使用with块的外部。

You can simply try the below code.您可以简单地尝试以下代码。 Your code did not work because you added the opening on file handler 'reportPaises' within the for loop.您的代码不起作用,因为您在 for 循环中添加了打开文件处理程序“reportPaises”。 You don't need to open the file handler again and again.您不需要一次又一次地打开文件处理程序。

Try re running your code line by line in the python shell as it is very easy to debug the bugs in the code.尝试在 python shell 中逐行重新运行您的代码,因为调试代码中的错误非常容易。

The below code will work下面的代码将工作

with open('something.txt') as f:
info =  f.readlines()
reportePaises = open('reportePaises.txt', 'w')
for item in info:
    reportePaises.write("%s" % item)

You don't need to add a \\n to the output line because when you perform readlines, the \\n character is preserved in the info list file.您不需要在输出行中添加 \\n,因为当您执行 readlines 时,\\n 字符会保留在信息列表文件中。 Please look observe below.请看下面的观察。

Try below试试下面

with open('something.txt') as f:
    info =  f.readlines()
print info

The output you will get is你会得到的输出是

['Bruce\n', 'brucechungulloa@outlook.com']

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

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