Stack Overflow Asked by dataplumber on November 16, 2021
I am trying to insert data to oracle db and the values for the columns are consumed from a list. the list contains several values and for empty values (marked as "" – Double Quotes) it fails.
So I would like to change all double quotes in a list to single quotes.
values = ['50.38146', "", "", 'ABC']
Here "" is marked as empty value. I would like to change all Double Double Quotes with Double Single Quotes to represent empty string. Something like this :
values = ['50.38146', '', '', 'ABC']
I tried below but doesn’t works :
row1 = []
for x in values:
if x == "":
row1.append('')
else:
row1.append(x)
Any ideas
The reason your code doesn't work is because in the code, you wrote, if x == "",
you are saying that if x equals blank. That doesn't do anything because you are saying, if x equals blank, instead, you need to use x == '""', this tells the program that you want to find the double quotes, and using the single quotes as the string specifier.
So, your code would be this instead:
row1 = []
for x in values:
if x == '""':
row1.append('')
else:
row1.append(x)
Answered by Phasakorn Chivatxaranukul on November 16, 2021
Get first index of "" and replace it with '' it will replace all the occurences of "" with ''
>>> values = ['50.38146', "", "", 'ABC']
>>> index=values.index("") #get first index of ""
>>> values[index]='' #replaces all "" with ''
>>> values
['50.38146', '', '', 'ABC']
Answered by user13966865 on November 16, 2021
Solution is to replace if x == "" with if x == '""'
row1 = []
for x in values:
if x == '""':
row1.append('')
else:
row1.append(x)
print(row1)
values = ['50.38146', '', '', 'ABC']
Answered by dataplumber on November 16, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP