1

I am currently joining two lists together in python to generate a string for input into a database using this code:

newDataString = ', '.join("%s=\'%s\'" % t for t in zip(tableColumns,data))

where tableColumns is a list of columns, and data is a list of data related to the columns. I am joining these together to perform an update.

It all works fine, except in cases where a value in the data list = None

cur.execute("UPDATE "+table+" SET "+str(newDataString)+" FROM "+tempTable+" WHERE "+generatePrimaryKeyMatches(primaryKey, table))
psycopg2.DataError: invalid input syntax for type date: "None"
LINE 1: ...ION RESEARCH & REP', suff='None', hon='None', dob='None', na...

I suppose I need an if statement in here somewhere to check every value in data before I put it into the join, and it should be treated differently (i.e. no \') if data = None, but I cannot for the life of me figure it out.

Any help greatly appreciated.

2
  • if it is none what u want to return Commented May 12, 2014 at 12:18
  • Hi Sundar I want to return the string as it would be in the case above, but without the apostrophes around it. e.g.: tableColumns = ['name', 'dob'] data = ['roger', None] would return: name='roger', dob=None Hope this clarifies. Commented May 12, 2014 at 12:21

1 Answer 1

1

I think this would be easier if you split it into two steps:

newDataList = []
for t in zip(tableColumns, data):
    if t[1] is not None:
        newDataList.append("{0}=\'{1}\'".format(*t))
    else:
        # ...deal with None
newDataString = ", ".join(newDataList)

Note I have switched the old-fashioned % formatting to str.format.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi John, this looks great. I've done a quick test and it looks like this may solve the problem. I'm just running a full test now so fingers crossed this is the last piece of the puzzle I need... if so... I owe you a beer. A huge cold beer. Will keep you posted...
Hi John, thank you so much for this, looks to have resolved the issue. One other query (if you could..!) I'm now having problems with strings that have apostrophes in them closing the data off early, for example...data='18221', datadesc='MANUFACTURE OF OTHER MEN'S OUTERWEA..., any idea how I could include str.replace into this functionality to remove all apostrophes from the data strings before writing to the newDataString? Best, Joe.
You could do for col, data in zip(...) then use .format(col, data.replace("'", "")) instead of simply data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.