Reputation: 333
Beginning Python guy here. Have some code I need help with.
My main question here is in this bit of code we have 3 define statements for mm, yy, and yyyy. In the 'hpds_folder =' statement it references 'dnb*{0}{1}' with the {0} and {1} being the 1st and 2nd input parameters.
mm = hadoop.date_part()['mm']
yy = hadoop.date_part()['yy']
yyyy = hadoop.date_part()['yyyy']
hdfs_folder = '/sandbox/US_MARKETING/COMMON_DATA/BAU/FILES/{0}/{1}'.format(yyyy, mm)
find_dnb = hadoop.file_find(file='dnb*{0}*{1}*'.format(mm, yy), folder = hadoop._xfer_in_hadoop['dnb'])
print('dnb*{0}*{1}*')
I'm assuming {0} and {1} should be what are populated by mm and yy respectively.
But when I try to print out the string for it: print('dnb*{0}{1}')
I get just the literal 'dnb*{0}{1}' as output.
Shouldn't I get a month and a year?
Upvotes: 0
Views: 32
Reputation: 2118
On your print statement, you didn't format the text, so it wasn't replaced. the assignment on file happened once and didn't change the string literal for other locations.
Therefore, your print should be formatted as well:
print('dnb*{0}*{1}*'.format(mm, yy))
In Python3.6+, a new formatted strings were introduced, letting you do things such as:
print(f'dnb*{mm}*{yy}*')
Notice the f
before the string mark. fstrings let you inject code to the string inside curly brackets {}
.
You can also use it on your find_dnb line:
find_dnb = hadoop.file_find(file=f'dnb*{mm}*{yy}*', folder = hadoop._xfer_in_hadoop['dnb'])
Upvotes: 1