简体   繁体   中英

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)

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

In both files.

You were having problems because every time you open a file with 'w' flag, you overwrite it on the disk. So, you created a new file every time.

You should open the second file only once, in the with statement:

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.

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). Instead, you treat the input file as an iterator and just read next line directly from your HDD on each iteration.

You also (if I did the testing right) don't need to append '\\n' to every line. The newlines are already in item . Because of that you don't need to use string formatting at all, just 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.

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. 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.

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. 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.

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. 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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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