Joseph
Joseph

Reputation: 49

How can we write a text file from variable using python?

I am working on NLP project and have extracted the text from pdf using PyPDF2. Further, I removed the blank lines. Now, my output is being shown on the console but I want to populate the text file with the same data which is stored in my variable (file).

Below is the code which is removing the blank lines from a text file.

for line in open('resume1.txt'):
  line = line.rstrip()
  if line != '':
    file=line
    print(file)

Output on Console: Eclipse, Visual Studio 2012, Arduino IDE, Java , HTML, CSS 2013 Excel .

Now, I want the same data in my (resume1.txt) text file. I have used three methods but all these methods print a single dot in my resume1.txt file. If I see at the end of the text file then there is a dot which is being printed.

Method 1:

with open("resume1.txt", "w") as out_file:
    out_file.write(file)

Method 2:

print(file,  file=open("resume1.txt", 'w'))

Method 3:

pathlib.Path('resume1.txt').write_text(file)

Could you please be kind to assist me in populating the text file. Thank you for your cooperation.

Upvotes: 1

Views: 1718

Answers (2)

Randall Buckton
Randall Buckton

Reputation: 25

First post on stack, so excuse the format

new_line = ""

for line in open('resume1.txt', "r"):
    for char in line:
        if char != " ":
            new_line += char
print(new_line)



with open('resume1.txt', "w") as f:
    f.write(new_line)

Upvotes: 0

DonLarry
DonLarry

Reputation: 945

First of all, note that you are writing to the same file losing the old data, I don't know if you want to do that. Other than that, every time you write using those methods, you are overwriting the data you previously wrote to the output file. So, if you want to use these methods, you must write just 1 time (write all the data).

SOLUTIONS

Using method 1:

to_file = []

for line in open('resume1.txt'):
    line = line.rstrip()
    if line != '':
        file = line
        print(file)
        to_file.append(file)

to_save = '\n'.join(to_file)

with open("resume1.txt", "w") as out_file:
    out_file.write(to_save)

Using method 2:

to_file = []

for line in open('resume1.txt'):
    line = line.rstrip()
    if line != '':
        file = line
        print(file)
        to_file.append(file)

to_save = '\n'.join(to_file)

print(to_save, file=open("resume1.txt", 'w'))

Using method 3:

import pathlib

to_file = []

for line in open('resume1.txt'):
    line = line.rstrip()
    if line != '':
        file = line
        print(file)
        to_file.append(file)

to_save = '\n'.join(to_file)

pathlib.Path('resume1.txt').write_text(to_save)

In these 3 methods, I have used to_save = '\n'.join(to_file) because I'm assuming you want to separate each line of other with an EOL, but if I'm wrong, you can just use ''.join(to_file) if you want not space, or ' '.join(to_file) if you want all the lines in a single one.

Other method

You can do this by using other file, let's say 'output.txt'.

out_file = open('output.txt', 'w')

for line in open('resume1.txt'):
    line = line.rstrip()
    if line != '':
        file = line
        print(file)
        out_file.write(file)
        out_file.write('\n') # EOL

out_file.close()

Also, you can do this (I prefer this):

with open('output.txt', 'w') as out_file:
    for line in open('resume1.txt'):
        line = line.rstrip()
        if line != '':
            file = line
            print(file)
            out_file.write(file)
            out_file.write('\n') # EOL

Upvotes: 1

Related Questions