Export process information to a .txt - Python

0

Good morning!

I have a problem exporting the information that my process is generating. Currently I copy everything that is in my print and paste it into a txt file, but I would like to take this manual process from my project.

I wanted to know if I could directly insert the generated values into a .txt on my desktop for example. In case I called it in the test.txt code

Code:

 f = open(r"C:\Users\guilgig\Desktop\test.txt", "w")
with open(r'C:\Users\guilgig\Desktop\Respostas.txt') as stream:
    for line in stream:
        for word in ['(ANUIDADE', 
'alta,,anuidade', 
'ANUID', 
'ANUIDA', 
'ANUIDAD', 
'ANUIDADE', 
'ANUIDADE)', 
'anuidade,', 
'anuidade,vcs', 
'anuidade.', 
'ANUIDADES', 
'ANUIDADR6', 
'POUCOS(EMPRESARIOS']:
            if word.lower() in line.lower():
                print(line.strip(), '¬', word)

                break

This code helped me create thanks too!

    
asked by anonymous 20.03.2018 / 15:03

2 answers

0

I was able to do this first part, now I have a problem of breaking lines, I should break every variable made, but everything comes in the same line.

Answer:

     f = open(r"C:\Users\guilgig\Desktop\test.txt", "w")
with open(r'C:\Users\guilgig\Desktop\Respostas.txt') as stream:
    for line in stream:
        for word in ['(ANUIDADE', 
'alta,,anuidade', 
'ANUID', 
'ANUIDA', 
'ANUIDAD', 
'ANUIDADE', 
'ANUIDADE)', 
'anuidade,', 
'anuidade,vcs', 
'anuidade.', 
'ANUIDADES', 
'ANUIDADR6', 
'POUCOS(EMPRESARIOS']:
            if word.lower() in line.lower():

              a = (line.strip(), '¬', word +'\n')

              arquivo.write(str(a))
              break
arquivo.close()
    
20.03.2018 / 20:21
0

@Gigle, you can concatenate your text as follows:

a = '%s%s%s\n' % (line.strip(), '¬', word)

I also suggest you move the list of words out of the loop to improve the performance and readability of your code.

The complete code looks like this:

words = ['(ANUIDADE', 
        'alta,,anuidade', 
        'ANUID', 
        'ANUIDA', 
        'ANUIDAD', 
        'ANUIDADE', 
        'ANUIDADE)', 
        'anuidade,', 
        'anuidade,vcs', 
        'anuidade.', 
        'ANUIDADES', 
        'ANUIDADR6', 
        'POUCOS(EMPRESARIOS']

f = open(r"C:\Users\guilgig\Desktop\test.txt", "w")
with open(r"C:\Users\guilgig\Desktop\Respostas.txt") as stream:
    for line in stream:
        for word in words:
            if word.lower() in line.lower():
                a = '%s%s%s\n' % (line.strip(), '¬', word)
                arquivo.write(a)
                break
arquivo.close()
    
23.10.2018 / 00:28