Home > other >  Error of unsupported operand type for: str and int even though the type is str and str
Error of unsupported operand type for: str and int even though the type is str and str

Time:12-11

I am reading data table from pdf and storing it in 2 arrays, dates = [1997, 1998] and prices = [3.45, 2.10, 1.89, ...] then I am formatting the dates in storing it in the dictionary as '1997,01', '1997,02'... and the Values accordingly. In the end I will send the

I am having problem with ex1_b(date: str) function where I try to change the date format to year-month. For example when I pass to the function "1997,01" it should return 1997-Dec

It gives out an error that I am trying to add str with int, but when I checked what type the years and months returns inside the function, it returns str. So I don't understand what I am doing wrong

Traceback (most recent call last):
  File "c:\Users\User\Downloads\Telegram Desktop\trypdf.py", line 55, in <module>
    ex1_b("2000,02")
  File "c:\Users\User\Downloads\Telegram Desktop\trypdf.py", line 51, in ex1_b
    date_form = str(years)   "-"   str(mon[months-1])
TypeError: unsupported operand type(s) for -: 'str' and 'int'

How can I solve this?

My code:

# THE BEGINNING IN CASE IT IS IMPORTANT

import PyPDF2
import numpy as np
import pandas as pd

pdf = (open("hw.pdf", "rb"))
pdfReader = PyPDF2.PdfFileReader(pdf)
page_object = pdfReader.getPage(4)
text = page_object.extractText()
splitted_text = text.split()

dates = []
prices = []

for i in range(4, len(splitted_text)):
    if float(splitted_text[i]) < 1997:
        prices.append(float(splitted_text[i]))
    else:
        dates.append(int(splitted_text[i]))
pdf.close()

df = {'Date' : [],
      'Values' : []}

for i in range(0, 25):
    for j in range(0, 12):
        month = str(j 1).zfill(2)
        df['Date'].append( str(dates[i])   ","   month)
        
for i in range(0,300):
    df['Values'].append(prices[i])

print(df)

mon = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
          'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

date_form = []

# THE PROBLEM
def ex1_b(date: str):

    years =  str(date.split(",")[0])
    months = str(date.split(",")[1])

    date_form = str(years)   "-"   str(mon[months-1])

    return date_form
ex1_b("2000,02")

CodePudding user response:

According to the following line, the value of the variable "months" is a string. Not an integer.

months = str(date.split(",")[1])

Later you subtracted 1 from the string month like this.

mon[months-1]

You can't use subtract operand with a string and an integer. Convert the variable "months" to an integer before subtracting 1.

ex: mon[int(months)-1]

  • Related