TypeError: sequence item 0: expected string, int found

| | | | | | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

I am attempting to insert data from a dictionary into a database. I want to iterate over the values and format them accordingly, depending on the data type. Here is a snippet of the code I am using:

def _db_inserts(dbinfo):
    try:
        rows = dbinfo["datarows"]

        for row in rows:
            field_names = ";".join([""{0}"".format(x) for x in row.keys()])
            value_list = row.values()

            for pos, value in enumerate(value_list):
                if isinstance(value, str):
                    value_list[pos] = ""{0}"".format(value)
                elif isinstance(value, datetime):
                    value_list[pos] = ""{0}"".format(value.strftime("%Y-%m-%d"))

            values = ";".join(value_list)

            sql = "INSERT INTO table_foobar ({0}) VALUES ({1})".format(field_names, values)

    except Exception as e:
        print "BARFED with msg:",e

When I run the algo using some sample data (see below), I get the error:

TypeError: sequence item 0: expected string, int found

An example of a value_list data which gives the above error is:

value_list = [377, -99999, -99999, "f", -99999, -99999, -99999, 1108.0999999999999, 0, "f", -99999, 0, "f", -99999, "f", -99999, 1108.0999999999999, -99999, "f", -99999, "f", -99999, "f", "f", 0, 1108.0999999999999, -99999, -99999, "f", "f", "f", -99999, "f", "1984-04-02", -99999, "f", -99999, "f", 1108.0999999999999] 

What am I doing wrong?

👻 Read also: what is the best laptop for engineering students?

How do I calculate the date six months from the current date using the datetime Python module?

1 answers

Eef By Eef

I am using the datetime Python module. I am looking to calculate the date 6 months from the current date. Could someone give me a little help doing this?

The reason I want to generate a date 6 months from the current date is to produce a review date. If the user enters data into the system it will have a review date of 6 months from the date they entered the data.

478

Answer #1

I found this solution to be good. (This uses the python-dateutil extension)

from datetime import date
from dateutil.relativedelta import relativedelta

six_months = date.today() + relativedelta(months=+6)

The advantage of this approach is that it takes care of issues with 28, 30, 31 days etc. This becomes very useful in handling business rules and scenarios (say invoice generation etc.)

$ date(2010,12,31)+relativedelta(months=+1)
  datetime.date(2011, 1, 31)

$ date(2010,12,31)+relativedelta(months=+2)
  datetime.date(2011, 2, 28)

We hope this article has helped you to resolve the problem. Apart from TypeError: sequence item 0: expected string, int found, check other code Python module-related topics.

Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Marie Chamberlet

Shanghai | 2023-03-25

Simply put and clear. Thank you for sharing. TypeError: sequence item 0: expected string, int found and other issues with enum Python module was always my weak point 😁. Will get back tomorrow with feedback

Angelo Galleotti

Vigrinia | 2023-03-25

Simply put and clear. Thank you for sharing. TypeError: sequence item 0: expected string, int found and other issues with join was always my weak point 😁. I am just not quite sure it is the best method

Boris Zelotti

Boston | 2023-03-25

Thanks for explaining! I was stuck with TypeError: sequence item 0: expected string, int found for some hours, finally got it done 🤗. Will get back tomorrow with feedback

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically