I am new to Python and can't quite make my if-cases any shorter. Any ideas how to do that?
import csv
fileheader = csv.reader(open("test.csv"), delimiter=",")
# Defines the header of the file opened
header = fileheader.next()
# Loop into the file
for fields in fileheader:
# For each header of the file, it does the following :
# 1/ Detect if the header exists in the file
# 2/ If yes and if the cell contains datas, it's added to the payload that will be used for an HTTP POST request.
# 3/ If not, it doesn't add the cell to the payload.
if 'url_slug' in header:
url_slug_index = header.index('url_slug')
if fields[url_slug_index] == "":
print "url_slug not defined for %s" % fields[url_index]
else:
payload['seo_page[path]'] = fields[url_slug_index]
if 'keyword' in header:
keyword_index = header.index('keyword')
if fields[keyword_index] == "":
print "keyword not defined for %s" % fields[url_index]
else:
payload['seo_page[keyword]'] = fields[keyword_index]
@Nick Burns : Perfect, it made the trick thank you very much ! I am very new to programming and simple reflex as creating custom functions is not here yet...
@tobiak_k : Thanks for the tip! Indeed the name is odd, Ill change it on my script. Need to get used to conventions :)