0

I was use threading Pool for my script. I have working code for html table to json conversion.

I am using pandas for html table to json.

html_source2 = str(html_source1)

pool = ThreadPool(4) 

table = pd.read_html(html_source2)[0]
table= table.loc[:,~table.columns.str.startswith('Unnamed')]
d = (table.to_dict('records'))
print(json.dumps(d,ensure_ascii=False))
results = (json.dumps(d,ensure_ascii=False))

i want something like:

html_source2 = str(html_source1)

pool = ThreadPool(4) 

def abcd():
  table = pd.read_html(html_source2)[0]
  table= table.loc[:,~table.columns.str.startswith('Unnamed')]
  d = (table.to_dict('records'))
  print(json.dumps(d,ensure_ascii=False))
  results = (json.dumps(d,ensure_ascii=False))
3
  • 1
    What is the problem you are having? When converting to function. Commented Jul 31, 2019 at 9:33
  • since i am new python, i dont knw how do i convert it to function. Commented Jul 31, 2019 at 9:34
  • I don't understand, can't you just add an argument to the abcd() function i.e. def abcd(html_str): and have it return results? Commented Jul 31, 2019 at 9:34

2 Answers 2

1

You are almost there. You need to make the function take an input argument, here html_str and then have it return the results you need so you can use them outside the function.

html_source2 = str(html_source1)

pool = ThreadPool(4) 

def abcd(html_str):
    table = pd.read_html(html_str)[0]
    table= table.loc[:,~table.columns.str.startswith('Unnamed')]
    d = (table.to_dict('records'))
    print(json.dumps(d,ensure_ascii=False))
    results = (json.dumps(d,ensure_ascii=False))
    return results 

my_results = abcd(html_source2)

And remove the print call if you don't need to see the output in the function

Sign up to request clarification or add additional context in comments.

4 Comments

how can i use Pool to this?
What do you mean? Your example code has one input string, you wouldn't need a pool for that. If you show more code, and try to explain what the purpose of threading is, then I can help.
i want improve speed of executing,for that i want to use multithreading.
You aren’t going to speed up a single function call by multithreading it. That’s not how it works. Also in python multithreading is only really going to help with I/O bound tasks.
0

I guess you don't know much about functions, parameters and how to call functions read here https://www.w3schools.com/python/python_functions.asp

Consider reading it it's a short read.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.