0

I have written a function to create dynamic files and the file name will contain u 2 arguments that are passed to the function.

def formrequests(carrier,airport):
    s=requests.Session()
    r=s.get("http://www.transtats.bts.gov/Data_Elements.aspx?Data=2")
    soup=BeautifulSoup(r.text)
    viewstate_element=soup.find(id="__VIEWSTATE")
    viewstate=viewstate_element["value"]
    eventvalidation_element=soup.find(id="__EVENTVALIDATION")
    eventvalidation=eventvalidation_element["value"]
    r = s.post("https://www.transtats.bts.gov/Data_Elements.aspx?Data=2",
       data = (
           ("__EVENTTARGET", ""),
           ("__EVENTARGUMENT", ""),
           ("__VIEWSTATE", viewstate),
           ("__EVENTVALIDATION", eventvalidation),
           ("CarrierList", carrier),
           ("AirportList", airport),
           ("Submit", "Submit")
       )
   )
   f=open("C:\\Users\\JAYASHREE\\Desktop\\Data Analyst Nanodegree\\Data Wrangking with MongoDB\\carrier-airport data files\\%s-%s.html","w" %(carrier,airport))
   f.write(r.text)

While running this function it is throwing me the following error:

TypeError                                 Traceback (most recent call last)
<ipython-input-64-3323d46461b0> in <module>()
      3 for carrier in carriers:
      4     for airport in airports:
----> 5         formrequests(carrier,airport)

<ipython-input-63-258f8611f601> in formrequests(carrier, airport)
     17                    ("Submit", "Submit")
     18                   ))
---> 19     f=open("C:\\Users\\JAYASHREE\\Desktop\\Data Analyst Nanodegree\\Data Wrangking with MongoDB\\carrier-airport data files\\%s-%s.html","w" %(carrier,airport))
     20     f.write(r.text)

TypeError: not all arguments converted during string formatting

Please let me know how to solve it.

2 Answers 2

3

The line with the f=open format string is incorrect. Maybe try:

f=open("C:\\Users\\JAYASHREE\\Desktop\\Data Analyst Nanodegree\\Data Wrangking with MongoDB\\carrier-airport data files\\{0}-{1}.html".format(carrier,airport),"w")

The arguments were beyond where they should be.

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

Comments

3

Looks like you got the formatting wrong on that line. Change this:

f=open("C:\\Users\\JAYASHREE\\Desktop\\Data Analyst Nanodegree\\Data Wrangking with MongoDB\\carrier-airport data files\\%s-%s.html","w" %(carrier,airport))

to this:

f=open("C:\\Users\\JAYASHREE\\Desktop\\Data Analyst Nanodegree\\Data Wrangking with MongoDB\\carrier-airport data files\\%s-%s.html" %(carrier,airport),"w")

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.