Python function with multiple URLs as input, which outputs multiple DataFrames [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 2 years ago.
Improve this question
I'm trying to make a Python function that lets me input multiple URLs, and it will return multiple dataframes, see below.
def getdata(*urls):
for i in urls:
return pd.read_csv(i,skiprows=4)
derby20, derby19, derby18, derby17 = getdata('https://uk-air.defra.gov.uk/data_files/site_data/DESA_2020.csv',
'https://uk-air.defra.gov.uk/data_files/site_data/DESA_2019.csv',
'https://uk-air.defra.gov.uk/data_files/site_data/DESA_2018.csv',
'https://uk-air.defra.gov.uk/data_files/site_data/DESA_2017.csv')
However I get the following error: ValueError: too many values to unpack (expected 4).
Any idea how I can successfully implement this?
Thank you!

You're returning once and never going in function back again. You can use yield instead of return.
def getdata(*urls):
for i in urls:
yield pd.read_csv(i,skiprows=4)
See this:
>>> def foo():
... for i in range(3): yield i
...
>>> a, b, c = foo()
>>> a, b, c
(0, 1, 2)

Change the lines
for i in urls:
return pd.read_csv(i,skiprows=4)
to
return (pd.read_csv(i, skiprows=4) for i in urls)
because after the first return command your function will not continue, effectively breaking your loop after the first iteration.

Related

printing data member using class name [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 12 months ago.
Improve this question
class Acc:
id=121
def _init_(self,id):
self.id=id;
acc=Acc(111)
print(Acc.id)
When I try to run this code , the code runs fine and gives 121 as output when I directly call the data member 'id' using class name Acc and remove the object creation line from the code but the code gives error "TypeError: Acc() takes no arguments" when I include the second last line i.e. the object creation line in my code.
You need to use double underscores when creating constructor method:
...
def __init__(self, id):
...
In your question code you just created a method _init_.

Python how should I fix this if I call my function, it returns function object [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 tried to call my function here and it returns function get_song_input at 0x7ff9869ee050
whats wrong with my code? I put it in python visualizer it worked out fine.
Album = namedtuple('Album', 'id artist title year songs')
Song = namedtuple('Song', 'track title length play_count')
def get_song_input():
"""Prompt user input."""
songtrack = input('Enter the song\'s track:\n')
songtitle = input('Enter the song\'s title:\n')
songlength = input('Enter the song\'s length:\n')
songplaycount = input('Enter the song\'s play_count:\n')
song_info = Song(songtrack, songtitle, songlength, songplaycount)
return song_info
print(get_song_input)
output:
<function get_song_input at 0x7ff9869ee050>
The function definition does not execute the function body; this gets executed only when the function is called.
To call a function, use the function name followed by parenthesis:
def my_function():
print("Hello from a function")
my_function()
As others have stated, you need the brackets, i.e.:
print(get_song_input())

Getting, TypeError: 'int' object is not callable, when I'm trying to add a new element on a list [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 5 years ago.
Improve this question
list_personagens = []
nomeA = input("Insert the name\n")
papel_personagem1 = random.randint(0, 1)
personagem1 = People(nomeA, papel_personagem1)
list_personagens.append(papel_personagem1())
print()
Error:
Traceback (most recent call last):
lista_personagens.append(papel_personagem1())
TypeError: 'int' object is not callable
I'm trying to add a randomly generated int to the empty list I just created, however, it gives an error saying that the 'int' object is not callable.
EDIT: Found the error. i was calling papel_personagem1 as a fuction.
Well, the error is pretty descriptive - you are trying to call it.
Try this (without a pair of parenthesis):
list_personagens.append(papel_personagem1)
papel_personagem1() is treating papel_personagem1 as a function (calling it) when it's not, you should remove the parentheses in order to treat it like an int:
papel_personagem1 = random.randint(0, 1) # an int
list_personagens.append(papel_personagem1)

Syntax error using def on python [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 8 years ago.
Improve this question
My qestion is that my def kept on getting a invalid syntax.
l=0
a=1
b=1
i=int(input("How many iterations?" )
def mains():
while i>l;
l=l+1
a=a+b
print(a)
def mains()
Here is the error.
File "Fibonacci.py", line 6
def mains():
^
SyntaxError: invalid syntax
I am currently using 3.4.1 version of python.
Well firstly you have incomplete ) on the previous line:
i=int(input("How many iterations?" ))
^
and secondly, it should be : not ;:
while i>l:
and thirdly, to call a method, you just do:
mains()
not def mains()

Python Function not returning [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 8 years ago.
Improve this question
I'm new to Python and am learning how it works through doing the exercises from project euler. Problem 2 is focused on the Fibonacci sequence for which I've created this recursive function:
def CalcFb(start,end):
if (end<=4000000):
CalcFb(end,start+end)
else:
print "Returning:",end
return end
print "Answer: {0}".format(CalcFb(start,start+1))
When I run the program I get the following output:
Returning: 5702887
Answer: None
I'm calling the function with:
start=1
I don't understand why "None" is being printed it should have printed 5702887. Can someone please help me to understand why this is happeneing?
Thanks
Dan
You are missing the return statement in the if-clause:
if (end<=4000000):
return CalcFb(end,start+end)
Otherwise you call your function recursively but only the last call returns a value and the second-to-last one does not return anything.
You are not returning any value when recursing...
def CalcFb(start,end):
if (end<=4000000):
return CalcFb(end,start+end) ### this needs to return a value as well
else:
print "Returning:",end
return end
for me its return 2
>>> def CalcFb(start,end):
... if (end<=4000000):
... CalcFb(end,start+end)
... else:
... print "Returning:",end
... return end
...
>>>
>>> start=1
>>> print "Answer: {0}".format(CalcFb(start,start+1))
Returning: 5702887
Answer: 2
Check indent.

Categories

Resources