[Tutor] Variable and a raw string

David L Neil PyTutor at DancesWithMice.info
Mon Jan 20 23:44:29 EST 2020


On 21/01/20 4:30 PM, Alessandro Caudilho wrote:
> On Tue, 21 Jan 2020 10:15:16 +1300
> David L Neil via Tutor <tutor at python.org> wrote:
>> Did you re-type the code or did you copy-paste it from the editor into
>> this email msg???
>> (the mixture of quotation-marks/double-quotes and
>> apostrophe/single-quote in the err.msg, indicates that the above code is
>> NOT the original)
>>
>> 1 if the code is being run (by "user") from his/her home-directory (ie
>> /home/user) then all that may be omitted from the open()
>>
>> 2 if your intent is to separate each US State into its own directory,
>> then would "...US/Colorado..." make for a better stepwise-refinement?
>>
>> 3 otherwise if the "us" part is merely a label, perhaps to distinguish
>> from some "colorado" in another country, then please see previous responses.
>>
>> 4 Please review open-append in
>> https://docs.python.org/3/library/functions.html?highlight=open#open
>> (see also "absolute" and "relative", per (1) above). What happens if one
>> opens a file in a directory which does not (yet) exist? How does this
>> differ from attempting to open a file which does not exist (read) or
>> does not yet exist (append)? (experiment in the Python REPL)
> 
> Thanks David! I copy-pasted the code from my editor.
> 
> This code is a part of the one that uses weather API. When I call, it returns me number of bulk values with current weather forecasts. One of values is a ). I know that I can`t create file because of '/' in 'colorado/us'. But maybe is there some way to avoid removing or replacing slash in 'state'?
> 
> My fault - I should give more information on this in my first message


No problem - we live and learn. We try to be a friendly group but we are 
here to help learners/learning.
(also, please check email headers to ensure answers go to the list so 
that others might learn/contribute suggestions)

As you will see, others also suggest that the err.msg does not smell 
right...

Not sure what is meant by "One of values is a )." (above). Is the 
right-parenthesis a response from the API?

Also (sorry to keep grumbling), DuckDuckGo returned at least three 
different hits for "weather API" from the US alone. Which one?

Why not also supply the query you are using and the data-returned 
(sub-set, as appropriate), so that we can see what you're facing...


Sorry, but I don't think it can be done: the problem with the (above) 
specification is that a slash (/) has particular meaning within a Posix 
filename (even on MS-Windows which uses back-slashes within its native 
rules). I don't think that the slash can be "escaped".
(but better minds than mine may correct/elucidate...)


Three thoughts:

1 if the "/us" component does not add-value to your application
...cut it out => "out damn spot"*!

 >>> state = 'colorado/us'
 >>> state_only = state[ :-3 ]
'colorado'

- assuming that the '/us' suffix is entirely consistent across (your) 
data-set!

2 if you must keep the "us" within the directoryNM, then change 
("translate") the slash to an underline (or similar).

3 go through and manually bulk-create the 50+ directories, which will 
obviate the exception.

4 (who said I could count?)
create a dictionary with the API's labels as "keys", eg 'colorado/us', 
and your own preference (and legal directoryNM) as "values", eg 
'colorado' or 'CO'. Use that as a translation-table to construct legal 
paths from the API labels.


* Shakespeare reference (in a bid to appear learned???)
-- 
Regards =dn


More information about the Tutor mailing list