Python Forum
Why the save.txt part is not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why the save.txt part is not working
#1
hello. I have this py code. It translates some text into english (default lang).

But I don't know why the translation is not saved in save.txt file

I have an error:

Quote:File "J:\Python\google-translator-python-main\google-translator.py", line 19
contents = file.write(f'{translated.text}')
^
IndentationError: expected an indented block

THIS IS MY PY CODE


from googletrans import Translator

# taking some text from indonesian language
text = '''Python adalah bahasa pemrograman tingkat tinggi yang ditafsirkan yang dibuat pada akhir 1980-an, tetapi diimplementasikan pada Desember 1989 oleh Guido Van Rossum. Kata Python berasal dari ular. Menurut survei StackOverflow baru-baru ini, Python telah melampaui popularitas Java'''

translator = Translator()
dt = translator.detect(text)
print(dt)

translated = translator.translate(text)
print(translated.text)

with open("save.txt", mode="w") as file:  # See note 2.
contents = file.write(f'{translated.text}')
Reply
#2
Please post your code, using the Python tags. Likely you have an indentation problem
Melcu54 likes this post
Reply
#3
(May-30-2021, 04:38 PM)jefsummers Wrote: Please post your code, using the Python tags. Likely you have an indentation problem

hello @jefsummers. I changed to Python
Reply
#4
The last line should be indented like this:

with open("save.txt", mode="w") as file:  # See note 2.
    contents = file.write(f'{translated.text}')
Reply
#5
(May-30-2021, 05:06 PM)BashBedlam Wrote: The last line should be indented like this:

with open("save.txt", mode="w") as file:  # See note 2.
    contents = file.write(f'{translated.text}')

yes, I try, is not working. Is the same code as mine..
Reply
#6
If i do test get error from library AttributeError: 'NoneType' object has no attribute 'group'
pip uninstall googletrans
pip install googletrans==3.1.0a0
Test again,now work.
from googletrans import Translator

# taking some text from indonesian language
text = '''Python adalah bahasa pemrograman tingkat tinggi yang ditafsirkan yang dibuat pada akhir 1980-an, tetapi diimplementasikan pada Desember 1989 oleh Guido Van Rossum. Kata Python berasal dari ular. Menurut survei StackOverflow baru-baru ini, Python telah melampaui popularitas Java'''

translator = Translator()
dt = translator.detect(text)
print(dt)

translated = translator.translate(text)
print(translated.text)

with open("save.txt", mode="w") as file:
    contents = file.write(f'{translated.text}')
save.txt:
Output:
Python is a high-level interpreted programming language created in the late 1980s, but implemented in December 1989 by Guido Van Rossum. The word Python comes from snake. According to a recent StackOverflow survey, Python has surpassed Java's popularity
Melcu54 likes this post
Reply
#7
(May-30-2021, 06:24 PM)snippsat Wrote: If i do test get error from library AttributeError: 'NoneType' object has no attribute 'group'
pip uninstall googletrans
pip install googletrans==3.1.0a0
Test again,now work.
from googletrans import Translator

# taking some text from indonesian language
text = '''Python adalah bahasa pemrograman tingkat tinggi yang ditafsirkan yang dibuat pada akhir 1980-an, tetapi diimplementasikan pada Desember 1989 oleh Guido Van Rossum. Kata Python berasal dari ular. Menurut survei StackOverflow baru-baru ini, Python telah melampaui popularitas Java'''

translator = Translator()
dt = translator.detect(text)
print(dt)

translated = translator.translate(text)
print(translated.text)

with open("save.txt", mode="w") as file:
    contents = file.write(f'{translated.text}')
save.txt:
Output:
Python is a high-level interpreted programming language created in the late 1980s, but implemented in December 1989 by Guido Van Rossum. The word Python comes from snake. According to a recent StackOverflow survey, Python has surpassed Java's popularity
Reply
#8
(May-30-2021, 07:40 PM)Melcu54 Wrote:
(May-30-2021, 06:24 PM)snippsat Wrote: If i do test get error from library AttributeError: 'NoneType' object has no attribute 'group'
pip uninstall googletrans
pip install googletrans==3.1.0a0
Test again,now work.
from googletrans import Translator

# taking some text from indonesian language
text = '''Python adalah bahasa pemrograman tingkat tinggi yang ditafsirkan yang dibuat pada akhir 1980-an, tetapi diimplementasikan pada Desember 1989 oleh Guido Van Rossum. Kata Python berasal dari ular. Menurut survei StackOverflow baru-baru ini, Python telah melampaui popularitas Java'''

translator = Translator()
dt = translator.detect(text)
print(dt)

translated = translator.translate(text)
print(translated.text)

with open("save.txt", mode="w") as file:
    contents = file.write(f'{translated.text}')
save.txt:
Output:
Python is a high-level interpreted programming language created in the late 1980s, but implemented in December 1989 by Guido Van Rossum. The word Python comes from snake. According to a recent StackOverflow survey, Python has surpassed Java's popularity
Reply
#9
I find a new solution.

Step 1. Add the first 2 imports at the beginning of the script
Step 2. At the end of the script copy all the code from # Save File at the end of each text
Step 3. Change the print (translated.text)


import sys
import os

from googletrans import Translator

# taking some text from indonesian language
text = '''Python adalah bahasa pemrograman tingkat tinggi yang ditafsirkan yang dibuat pada akhir 1980-an, tetapi diimplementasikan pada Desember 1989 oleh Guido Van Rossum. Kata Python berasal dari ular. Menurut survei StackOverflow baru-baru ini, Python telah melampaui popularitas Java'''

translator = Translator()
dt = translator.detect(text)
print(dt)

translated = translator.translate(text)
print(translated.text)

# Save File at the end of each text

class Logger(object):
    def __init__(self, filename="Default.log"):
        self.terminal = sys.stdout
        self.log = open(filename, "a")

    def write(self, message):
        self.terminal.write(message)
        self.log.write(message)

    def flush(self):
        pass


path = os.path.abspath(os.path.dirname(__file__))	#Get the parent directory of the current py file
type = sys.getfilesystemencoding()
sys.stdout = Logger('save.txt')						#Output file

#The following print content will be output to a.txt

print(translated.text)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to save to multiple locations during save cubangt 1 554 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  Why isn't ImageGrab.save working SheeppOSU 0 1,964 Feb-18-2019, 06:39 AM
Last Post: SheeppOSU
  how to save python file to working directory rpdohm 3 6,010 Sep-14-2017, 06:16 PM
Last Post: rpdohm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020