Python Error when I m converting Excel to JSON

0 votes

Here's my code:

import json, xlrd
import datetime as dt

# This is the Excel data (no keys)
filename=r'C:\Users\timhe\OneDrive\Desktop\people_from_excel.json'

# Open the file (standard file open stuff)
with open(filename, 'r', encoding='utf-8-sig', newline='') as f:
    
    # Load the whole json file into an object named people
    people=json.load(f)
    
    print(people)
    
# Dictionaries are in a list, loop through and display each dictionary.
for p in people:
    name=p['Full Name']
    byear=p['Birth Year']
    
    # Excel date pretty tricky, use xlrd module.
    y, m, d, h, i, s=xlrd.xldate_as_tuple(p['Date Joined'], 0)
    joined=dt.date(y, m, d)
    balance='$'+f"{p['Balance']:,.2f}"
    print(f"{name:<22} {byear} {joined:%m/%d/%Y} {balance:>12}")

I converted an Excel data file to JSON and I'm getting this error when I run my code:

TypeError: '<' not supported between instances of 'str' and 'float'

I'm guessing somewhere a data type must change but I'm not sure where I need to make that change.

Feb 5, 2023 in Others by Kithuzzz
• 38,010 points
507 views

1 answer to this question.

0 votes

The "Birth Year" value in the p dictionary is a float, and you are attempting to compare it to a string in the line name=p['Full Name'], which may be why the problem is occuring.

Before utilising the birth year in the comparison, you must cast it to an integer or a string.

byear = int(p['Birth Year'])

or

byear = str(p['Birth Year'])
answered Feb 5, 2023 by narikkadan
• 63,420 points

Related Questions In Others

0 votes
1 answer

How to convert data from txt files to Excel files using python

Hi , there are few steps to ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,670 points
13,347 views
0 votes
1 answer

Convert JSON file to Excel File

Hi To convert a file, Set options and ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,670 points
996 views
0 votes
1 answer

Excel automatically converting 7 digit CAS number to another number (date?)

Looks like you could use: The formula in D2: =SUBSTITUTE(F ...READ MORE

answered Sep 25, 2022 in Others by narikkadan
• 63,420 points
604 views
0 votes
1 answer

Java Spring - Writing Excel file and converting to PDF

Since you are using Spring I suggest ...READ MORE

answered Sep 26, 2022 in Others by narikkadan
• 63,420 points
1,943 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,050 views
0 votes
1 answer
0 votes
1 answer

Convert CASE WHEN logic from SQL to EXCEL

A combination of IF(), ISNUMBER(SEARCH()), and OR() ...READ MORE

answered Sep 21, 2022 in Others by narikkadan
• 63,420 points
613 views
0 votes
1 answer

Print chosen worksheets in excel files to pdf in python

In the simplest form: import win32com.client o = win32com.client.Dispatch("Excel.Application") o.Visible ...READ MORE

answered Sep 24, 2022 in Others by narikkadan
• 63,420 points
2,734 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP