Posts: 63
Threads: 22
Joined: May 2021
May-30-2021, 04:27 PM
(This post was last modified: May-30-2021, 05:02 PM by Melcu54.)
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
from googletrans import Translator
text =
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}' )
|
Posts: 1,358
Threads: 2
Joined: May 2019
Please post your code, using the Python tags. Likely you have an indentation problem
Posts: 63
Threads: 22
Joined: May 2021
(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
Posts: 379
Threads: 2
Joined: Jan 2021
The last line should be indented like this:
1 2 |
with open ( "save.txt" , mode = "w" ) as file :
contents = file .write( f '{translated.text}' )
|
Posts: 63
Threads: 22
Joined: May 2021
(May-30-2021, 05:06 PM)BashBedlam Wrote: The last line should be indented like this:
1 2 |
with open ( "save.txt" , mode = "w" ) as file :
contents = file .write( f '{translated.text}' )
|
yes, I try, is not working. Is the same code as mine..
Posts: 7,326
Threads: 123
Joined: Sep 2016
If i do test get error from library AttributeError: 'NoneType' object has no attribute 'group'
1 2 |
pip uninstall googletrans
pip install googletrans = = 3.1 . 0a0
|
Test again,now work.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
from googletrans import Translator
text =
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
Posts: 63
Threads: 22
Joined: May 2021
(May-30-2021, 06:24 PM)snippsat Wrote: If i do test get error from library AttributeError: 'NoneType' object has no attribute 'group'
1 2 |
pip uninstall googletrans
pip install googletrans = = 3.1 . 0a0
|
Test again,now work.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
from googletrans import Translator
text =
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
Posts: 63
Threads: 22
Joined: May 2021
(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'
1 2 |
pip uninstall googletrans
pip install googletrans = = 3.1 . 0a0
|
Test again,now work.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
from googletrans import Translator
text =
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
Posts: 63
Threads: 22
Joined: May 2021
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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import sys
import os
from googletrans import Translator
text =
translator = Translator()
dt = translator.detect(text)
print (dt)
translated = translator.translate(text)
print (translated.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__))
type = sys.getfilesystemencoding()
sys.stdout = Logger( 'save.txt' )
print (translated.text)
|
|