Export process information to a . txt - Python

Asked

Viewed 375 times

0

Good morning!

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

I wanted to know if it is possible to insert directly the generated values in a .txt in my area of work for example. In case I called him 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 you helped me to create also thank you!

  • What exactly should this code do?

  • Gigle, would you happen to have any samples from the incoming file, and any samples from the outgoing file? So we can understand what are the expected inputs and outputs.

  • This project I look inside the text this list of words. the project itself works, but wanted to do what appears inside the PRINT log... do this inside a . txt I’m sorry, I forgot about the file I put. but it contains the following text: 2701 WHENEVER I NEEDED I WAS ATTENDED WITH ATHENA 6913 knowledge 11607 Evening attendance. attentive and competent ! 13286 GREAT SERVICE 14747 And I am satisfied . being Client

2 answers

0

Guys I managed 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()

0

@Gigle, you can concatenate your text as follows:

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

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

The whole code would look 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()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.