1

whenever I run this code it leaves a list that is like ['1999','2000',] and therefore I cannot perform an operation like ' m, b = np.polyfit(x, y, 1)'


def getCH4():
    years = []
    ch4 = []

    try:
        sqlConnect = sqlite3.connect('table1.db')
        cursor = sqlConnect.cursor()
        print("connected for reading")

        sqlite_SelectQuery = """SELECT * from datahub"""
        cursor.execute(sqlite_SelectQuery)
        records = cursor.fetchall()

        for row in records:
            years.append(row[0])
            ch4.append(row[2])

            cursor.close()

    except sqlite3.Error as error:
        print("Failed to read data from sqlite table", error)

    finally:
        if (sqlConnect):
            sqlConnect.close()
            print("The SQLite connection is closed")
    time.sleep(1)
    return years, ch4

  • 1
    Is it possible to convert the strings to ints before the values are appended to your result list(s)? (If those are strings instead of ints in the list result(s).) More info [here](https://docs.python.org/3/library/functions.html#int). – summea Mar 03 '21 at 04:12
  • You can accept the answer which you feel gives a solution to your question Thanks – Sivaram Rasathurai Mar 13 '21 at 01:46

2 Answers2

1

You can use map function to convert all elements of an iterable to a particular type

items = ['1', '2', '3', '4']
int_items = list(map(int, items))

Now int_items would be [1,2,3,4]

InsertCheesyLine
  • 1,112
  • 2
  • 11
  • 19
0

you can use map

strList = ['1','2','3','4']
intList = list(map(lambda x : int(x), strList))
Sivaram Rasathurai
  • 5,533
  • 3
  • 22
  • 45