Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
write to csv file problem
#6
Quote:ent from the output_found_flights gets returned as empty
Really? output_from_flights either returns a dictionary or it returns None. None is not the same as empty. {} is empty.

When you run your code, do you see the message box asking if it is ok to open the web browser? If you don't see that, do you see this error messge?
messagebox.showerror(title="Ooops",message="No flights were found")
I think it likely the query returns nothing and your code raises a key error when it tries to use flights['data']. Even if the request was successful I think there are several errors in the function that will raise exceptions. Verify that the request is made at all.

This makes no sense:
    try:
        lowest_price = round(float(ent["lowest_price"]), 2)
    except ValueError:
        messagebox.showerror(title="Ooops",
                             message="You have not entered appropriate input for the lowest price variable")
Popping up a message window is not handling the error. Immediately after this code you send your request and process the response even though you haven't assigned a value to lowest_price. This will result in the code raising a NameError when you try to use lowest_price in the function.

Python is not C, use iterators instead of indexing. Instead of this:
    for i in range(0, len(flights['data'])):
        flight_price = round(float(flights['data'][0]['price']), 2)  # <- Notice the index error.  Should be ['data'][i]
        if flight_price <= lowest_price:
Do this:
    for flight in flights["data"]:
        if flight["price"] <= lowest_price:
Looking at your code I think you are trying to do too much at once. Break up the program into smaller parts an solve them individually. For example, write a program that only does the request part. What happens if you make an invalid request? How do can you identify and correct the problem. How do you filter the responses? Currently you look for a price lower than your entered price. This might ignore lower prices. Once you are happy with how the request and filtering work you can fold it back into your program.
Reply


Messages In This Thread
write to csv file problem - by jacksfrustration - Nov-03-2023, 03:55 PM
RE: write to csv file problem - by noisefloor - Nov-03-2023, 05:05 PM
RE: write to csv file problem - by deanhystad - Nov-03-2023, 05:54 PM
RE: write to csv file problem - by deanhystad - Nov-03-2023, 10:22 PM
RE: write to csv file problem - by jacksfrustration - Nov-03-2023, 10:47 PM
RE: write to csv file problem - by deanhystad - Nov-04-2023, 05:43 AM
RE: write to csv file problem - by jacksfrustration - Nov-07-2023, 02:31 PM
RE: write to csv file problem - by deanhystad - Nov-07-2023, 03:55 PM
RE: write to csv file problem - by jacksfrustration - Nov-07-2023, 04:32 PM
RE: write to csv file problem - by deanhystad - Nov-07-2023, 06:30 PM
RE: write to csv file problem - by jacksfrustration - Nov-07-2023, 08:58 PM
RE: write to csv file problem - by deanhystad - Nov-09-2023, 01:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  What does .flush do? How can I change this to write to the file? Pedroski55 3 381 Apr-22-2024, 01:15 PM
Last Post: snippsat
  Last record in file doesn't write to newline gonksoup 3 542 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,723 Nov-09-2023, 10:56 AM
Last Post: mg24
  How do I read and write a binary file in Python? blackears 6 7,745 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,203 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Read text file, modify it then write back Pavel_47 5 1,785 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  how to read txt file, and write into excel with multiply sheet jacklee26 14 10,683 Jan-21-2023, 06:57 AM
Last Post: jacklee26
  How to write in text file - indented block Joni_Engr 4 6,623 Jul-18-2022, 09:09 AM
Last Post: Hathemand
  Upgrading from 2 to 3 and having file write problems KenHorse 2 1,559 May-08-2022, 09:47 PM
Last Post: KenHorse
  Cursor write 3rd file empty paulo79 3 1,965 Mar-10-2022, 02:51 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020