0

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?

1 Answer 1

1

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'])
Sign up to request clarification or add additional context in comments.

2 Comments

I See! That sums that one up! I added the formatting as you had said and the print statement ran correctly( Which btw was: 'dnb*08*20*')! Thanks a Bunch! Also I want to give you credit for answering this. How do I do that?
@Carbon Glad that it worked :) If this solution resolves your problem, there is a mark symbol under the upvote/downvote buttons on the left - You can press it and then it will become green. I'll be rewarded reputation and if people will encounter that post in the future, they'll be able to see what was the solution :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.