0

How to create a file.txt in python if it's object does not exist in an api response? My sample code as below; where for instance, departureGateDelayMinutes is missing from the response. And I want to insert 0 in the file for its absence.

url1 = 'Http://..........' json_obj1 = urllib2.urlopen(url1) data1 = json.load(json_obj1) try: with open("delays_details.txt",'a') as f: if (data1['flightStatus']['delays']['departureGateDelayMinutes']): f.write("Not Found") else: f.write(('%d' % item['flightId'])+'|'+str(data1['flightStatus']['delays']['departureGateDelayMinutes'])+'\n') except: print "Delay details Not Found! Try again."

1 Answers1

0

Check SQL - Replacing all "ASCII/special characters" in a string

-- create test table vc
create table vc(StringTest varchar(20))
insert vc values('StringÀÆ'), ('ÆStringÆ')
go

-- create test table CharacterMapping
create table CharacterMapping(SpecialCharacter char(1), MappedCharacter varchar(2))
insert CharacterMapping values('À', 'A'),('Æ', 'AE'), ('Ç', 'C')
go

--build the varchar for updating
declare @x varchar(max) = 'StringTest'
select @x = 'replace('+@x+', ''' + SpecialCharacter + ''','''+MappedCharacter+''')'  
from CharacterMapping
set @x = 'update vc set StringTest=' + @x +' from vc'

exec (@x)

select * from vc
Community
  • 1
  • 1