Issue with join() function? [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm attempting to bulk-download some csv files from a website. I've included a generic form of the first few lines of code I'm using below.
import urllib3
import os.path
def downLoadToDir(save_path,foo):
http = urllib3.PoolManager()
os.makedirs("".join(save_path, foo)
# Set up url and path for download
VarUrl = "".join("http://url.com/ajax/exportKR2CSV.html?t=", foo)
VarPath = "".join(save_path, foo, '/',foo, '.csv')
Ideally this should set up a folder under the specified filepath, and set up two variables I use later. However, I keep getting this error:
File "url_download.py", line 10
VarUrl = "".join("url.com/ajax/exportKR2CSV.html?t=", foo)
^
SyntaxError: invalid syntax
Based off of other examples I've seen online, this seems correct to me. Nothing seems to make it happy. Where am I going wrong? Thanks

You're missing a right parenthesis at line:
os.makedirs("".join(save_path, foo)
Also, the join method takes only one list argument, and you are passing two arguments here. You should make the two strings a list before passing to join as one argument:
os.makedirs("".join([save_path, foo]))
The same issue goes for the following lines that also use join.

You are not closing the bracket on line 7. Python is still looking for the close bracket and the "" is not what the compiler expects to see.
use os.makedirs("".join(save_path, foo))

Related

Error on python - TypeError: 'str' object is not callable [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
I'm starting to code right now, but I've searched on google and found an answer: I know the problem is a variable that is already predefined in python that needs to be renamed, but I can't find it. Can someone help me?
import os
import pandas as pd
lista_arquivo = os.listdir(fr"C:\Users\Master\Desktop\cursos\projetos\PYTHON\Faturamento_AM")
print(lista_arquivo)
tabela_total = pd.DataFrame()
for arquivo in lista_arquivo:
if "abril.xlsx" in arquivo():
tabela = pd.read_excel(fr"C:\Users\Master\Desktop\cursos\projetos\PYTHON\Faturamento_AM\{arquivo}")
tabela_total = tabela_total.append(tabela)
print(arquivo)
print(tabela_total)
tabela_faturamento = tabela_total.groupby('Faturamento').sum()
print(tabela_faturamento)
TypeError: 'str' object is not callable
I tried renaming the file, putting 'r', 'f' before the directory path, putting {file} at the end of the directory path...
you have a typo in
if "abril.xlsx" in arquivo():
it should be:
if "abril.xlsx" in arquivo:
When you are adding () to the variable name it is trying to "call" it - execute as a function, but it is string, that's why you're getting error

Python, unexpected EOF while parsing error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am a complete beginner in Python, having used R quite a lot before.
I am trying to write a multiline string which is very straightforward in R but I receive an error that stumps me, I have googled around everywhere but the suggested solutions have not worked.
When I try to use parentheses as I have seen suggested:
multiline = ("Hello,"
" my name is"
" James")
I receive:
multiline = ("Hello,"
File "<ipython-input-56-f67f7efad636>", line 1
multiline = ("Hello,"
^
SyntaxError: unexpected EOF while parsing
Similarly I tried triple quotes, also suggested:
multiline = """Hello,
my name is
James"""
I receive:
multiline = """Hello,
File "<ipython-input-58-0879a928a2ee>", line 1
multiline = """Hello,
^
SyntaxError: EOF while scanning triple-quoted string literal
I am sure I will be missing something blindingly obvious to more experienced Python users but any help would be greatly appreciated.
Thanks very much
Apparently, this is neither an issue with python, ipython or spyder. The mistake is using the incorrect command to run your code. You probably used Run Selection or Current Line, while the cursor was in the first line of the multiline strings. This command doesn't consider the context, so it essentially pastes the line into the interpreter and runs. Your first error was caused by python not finding the closing parentheses. Your second error is python not finding the closing triple quotes. In spyder, one typically makes code cells with #%% and runs them with CTRL+ENTER or SHIFT+ENTER. That should fix your problem.

I get a syntax error when trying to translate into broken english using Googletrans API (Python 3.8.1)? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
This is very much a dumb thing to ask, however I'm new to Python. I just installed googletrans from PyPI and I wanted to make a program that runs a phrase through a set of translations to make the phrase come out as broken English. However, I get a Syntax error! I can't figure it out and I've Googled everywhere. Please help!
from googletrans import Translator
import os
translator = Translator()
def addToClipBoard(text):
command = 'echo ' + text.strip() + '| clip'
os.system(command)
one = (translator.translate(input("ENTER PHRASE"))
two = (translator.translate(one, dest='sp'))
three = (translator.translate(two, dest='ch'))
four = (translator.translate(three, dest='fi'))
five = (translator.translate(four, dest='ja'))
result = (translator.translate(five))
addToClipBoard(result.text)
print("Copied succesfully. Closing now...")
exit()
You're missing a bracket at the end here:
one = (translator.translate(input("ENTER PHRASE")) # <- missing bracket
# should be
one = (translator.translate(input("ENTER PHRASE"))) # <- see the extra bracket
I recommend using a linter, like pylint. Linters catch syntax/style errors for you. If you use a text editor like vscode or pycharm, it's really easy to have it work in the background while you code so you can fix your syntax errors while coding.

Reverse a string with users input [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I decided to take on some programming projects and decided to follow this guide:
https://www.dreamincode.net/forums/topic/78802-martyr2s-mega-project-ideas-list/
I started with the first project, which is to reverse a string, pretty simple stuff as you can find out how to do that in the documentation. IE.
txt = "Hello World"[::-1]
print(txt)
dlroW olleH
s = "health" [::-1]
print(s)
htlaeh
And So on... There's two questions really. In this guide i'm not to sure if they just wanted you to reverse the string. It seems like they want Input? For the user to Enter the String.
In this case i thought it might be something like this:
string = Input("Enter a string")
print(s [::-1])
How ever this did not work, Not to sure how to implement this in Python 3.8.2. Could someone let me know and how they did it that would be great.
Thank you.
Your code has a space between s and the indices you want. You also have two variable names, s and string. Choose one. Finally, Input must be lowercase. Change it to this:
string = input("Enter a string")
print(string[::-1])
Your print statement needs some modifications.
Here it goes-
print(string[: :-1])

re.findall working in console but not in script? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm probably missing something very basic here, but here goes:
I'm using Python 2.7 and regex to identify digits within a string.
In the console, I type in:
>>> newstr = 'NukeNews/File_132.txt'
>>> int(re.findall(r'\d+',newstr)[0])
132
Which is what I expect.
However, in the script I'm running, I have the strings stored in a dictionary, linedict. I'm running this script:
news_id=[]
for line in line_vec:
print linedict[line]
newstr= linedict[line]
id_int = re.findall('r\d+',newstr)
print id_int
news_id.append(id_int)
It's a long list, but the output looks like:
NukeNews/File_132.txt
[]
So - the correct string is registered, but it's not matching on anything.
I was calling the first item in the list earlier (to match the console input of int(re.findall(r'\d+',newstr)[0]), but the script is telling me that the regex didn't find any instances of the digits in the string. I would expect this to return:
NukeNews/File_132.txt
['132']
Any idea why it's not working as expected? When I try running re.match(r'/d+',newstr) I also get an empty group (following the groups example on https://docs.python.org/2/library/re.html).
Edit: As pointed out, this is a case of not being careful with 'r' and r'*'. I'm just going to leave this up in case anyone else googling "why does my regex work in console but not in script" forgets to check this typo, like I did.
You've got your r inside the quotes so instead of getting a "raw string" you're getting a string with an 'r' in it ...
id_int = re.findall('r\d+',newstr)
# ^
# should be:
id_int = re.findall(r'\d+',newstr)
your "console" version also only takes the first of the found matches compared to your "script" version which appends the entire list.

Categories

Resources