Re: Trying to understand nested loops

2022-08-05 Thread Mladen Gogala via Python-list
On Fri, 5 Aug 2022 08:34:45 +0100, ojomooluwatolami675 wrote:

> Hello, I’m new to learning python and I stumbled upon a question nested
> loops. This is the question below. Can you please how they arrived at 9
> as the answer. Thanks
> 
> var = 0 for i in range(3):
>   for j in range(-2,-7,-2):
> var += 1
>  print(var)
> 
> Sent from my iPhone

That's simple. Number 9 itself is equal to 3*3. Inner loop executes 3 
times for each of the 3 executions of the outer loop.
PS:
The reply is intentionally written in such a way that it cannot be used as 
an answer to a homework question. You'll have to work out the details 
yourself.

-- 
Mladen Gogala
Database Consultant
https://dbwhisperer.wordpress.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Avoid nested SIGINT handling

2021-11-13 Thread Mladen Gogala via Python-list
On Thu, 11 Nov 2021 17:22:15 +1100, Chris Angelico wrote:

> Threads aren't the point here - signals happen immediately.

Actually, signals are not delivered immediately. Signals are delivered 
the next time the process gets its turn on CPU. The process scheduler 
will make process runnable and the process will check for any pending 
signals first and will execute the handler. It is possible to have 
several SIGINT signals pending, for instance when I nervously press ctrl-
C several times. However, signals are not processed as a part of the 
normal flow of the process and are processed sequentially.. When the 
process finds a pending signal, it executes the registered signal 
handler. It's always the same signal handler, unless signal handler is 
changed within the signal handler. After the signals are delivered, the 
process continues its normal operation until its CPU quantum expires or 
until initiates a synchronous I/O operation, as is the case with all 
normal read operations.
BTW, that's the case on both Unix/Linux systems and Windows systems.



-- 
Mladen Gogala
Database Consultant
https://dbwhisperer.wordpress.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Website

2021-04-14 Thread Mladen Gogala via Python-list
On Wed, 14 Apr 2021 15:41:37 +0200, Rainyis wrote:

> Hello,
> I am Sergio Llorente, and I want to create a web about python. I will
> publish apps, scripts.. made by python. I will like to put python in the
> domain. The domain will be like all-about-python.com but in Spanish(
> todosobrepython.com). Can I use it?
> 
> Thanks in advance,
> Sergio

I give you my permission. May the Force be with you and your website.



-- 
Mladen Gogala
Database Consultant
https://dbwhisperer.wordpress.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: error of opening Python

2021-02-26 Thread Mladen Gogala via Python-list
On Fri, 26 Feb 2021 02:23:51 -0500, Terry Reedy wrote:


> Not on Windows.  Please don't spew misleading garbage that will only
> confuse the new user on a different operating system.
>

You are right, I apologize. I sort of like poking fun at the Winduhs 
users but this is not the right place.



-- 
Mladen Gogala
Database Consultant
https://dbwhisperer.wordpress.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: error of opening Python

2021-02-25 Thread Mladen Gogala via Python-list
On Thu, 25 Feb 2021 17:22:35 +, Botao Liu wrote:

> Dear Python team,
> 
> This is my first time using Python, I tried to launch Python and it
> showed "Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC
> v.1928 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or
> "license" for more information." I don't know what this meant and how to
> fix this. Could you please help me? Thank you very much.
> 
> Kind regards,
> 
> Botao Liu

Try with this first: https://python.swaroopch.com/
It gives you the details of what that means and what to do next. Also, 
your Python interpreter is broken it says:


It should say something like this:
Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC
> v.1928 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or
> "license" 

-- [mgogala@umajor ~]$ python3
Python 3.9.1 (default, Jan 20 2021, 00:00:00) 
[GCC 10.2.1 20201125 (Red Hat 10.2.1-9)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

The problem string is "win32". You should be able to open a terminal 
Window and execute "uname -r".


Mladen Gogala
Database Consultant
https://dbwhisperer.wordpress.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: PSYCOPG2

2021-02-13 Thread Mladen Gogala via Python-list
I don't have PSYCOPG2 on my system, but try doing the following from 
python:
[mgogala@umajor ~]$ python3
Python 3.9.1 (default, Jan 20 2021, 00:00:00) 
[GCC 10.2.1 20201125 (Red Hat 10.2.1-9)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>> dir(cx_Oracle)
['ATTR_PURITY_DEFAULT', 'ATTR_PURITY_NEW', 'ATTR_PURITY_SELF', 'ApiType', 
'BFILE', 'BINARY', 'BLOB', 'BOOLEAN', 'Binary', 'CLOB', 'CURSOR', 
'Connection', 'Cursor', 'DATETIME', 'DBSHUTDOWN_ABORT',

 'DBSHUTDOWN_FINAL', 'DBSHUTDOWN_IMMEDIATE', 'DBSHUTDOWN_TRANSACTIONAL', 
'DBSHUTDOWN_TRANSACTIONAL_LOCAL', 'DB_TYPE_BFILE', 
'DB_TYPE_BINARY_DOUBLE', 'DB_TYPE_BINARY_FLOAT', 
'DB_TYPE_BINARY_INTEGER', 'DB_TYPE_BLOB', 'DB_TYPE_BOOLEAN', 
'DB_TYPE_CHAR', 'DB_TYPE_CLOB', 'DB_TYPE_CURSOR', 'DB_TYPE_DATE', 
'DB_TYPE_INTERVAL_DS', 'DB_TYPE_INTERVAL_YM', 'DB_TYPE_JSON', 
'DB_TYPE_LONG', 'DB_TYPE_LONG_RAW', 'DB_TYPE_NCHAR', 'DB_TYPE_NCLOB', 
'DB_TYPE_NUMBER', 'DB_TYPE_NVARCHAR', 'DB_TYPE_OBJECT', 'DB_TYPE_RAW', 
'DB_TYPE_ROWID', 'DB_T
...

>>> conn=cx_Oracle.Connection("scott/tiger@ora19c")
>>> 
Please use PSYCOPG2 instead of cx_Oracle. After you do it 
from python3 interpreter do it from Idle:



On Sat, 13 Feb 2021 22:08:11 +, Tony Ogilvie wrote:

> Thank you
> 
> I have tried Sublime 3 and the same thing happens. I do not think I have
> another version of Python on my PC. I am trying to look through my files
> to find out.
> 
> Regards
> 
> Tony
> 
> 
> -Original Message-
> From: Mladen Gogala 
> Sent: 13 February 2021 05:35 To: python-list@python.org Subject: Re:
> PSYCOPG2
> 
> On Fri, 12 Feb 2021 18:29:48 +, Tony Ogilvie wrote:
> 
>> I am trying to write a program to open a PostgesSQL 13 database using
>> psycopg2. All seems to work if I write direct to Python but if I write
>> the script into IDLE it does not work with the IDLE Shell 3.9.1
>> reporting an error of no attribute 'connect'.
>> 
>> 
>> 
>> I have tried many options to try and get this to work.
>> 
>> 
>> 
>> Regards
>> 
>> 
>> 
>> Tony
> 
> It looks like your idle is not using the same interpreter that you are
> using when writing direct code. Anyway, my advice would be to ditch Idle
> and use VSCode. It's fabulous.





-- 
Mladen Gogala
Database Consultant
https://dbwhisperer.wordpress.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: PSYCOPG2

2021-02-12 Thread Mladen Gogala via Python-list
On Fri, 12 Feb 2021 18:29:48 +, Tony Ogilvie wrote:

> I am trying to write a program to open a PostgesSQL 13 database using
> psycopg2. All seems to work if I write direct to Python but if I write
> the script into IDLE it does not work with the IDLE Shell 3.9.1
> reporting an error of no attribute 'connect'.
> 
> 
>  
> I have tried many options to try and get this to work.
> 
> 
>  
> Regards
> 
> 
>  
> Tony

It looks like your idle is not using the same interpreter that you are 
using when writing direct code. Anyway, my advice would be to ditch Idle 
and use VSCode. It's fabulous.



-- 
Mladen Gogala
Database Consultant
https://dbwhisperer.wordpress.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Issues with running python in Command prompt

2021-01-20 Thread Mladen Gogala via Python-list
On Wed, 20 Jan 2021 08:33:40 -0700, Michael Torrie wrote:

> You can use escape characters in regular expression on any platform
> Python runs on.  Python has a whole host of features to deal with the
> directory separator differences on Windows vs everybody else.

I am probably slightly biased. I am an old Unix hack who has learned Perl4 
in the early 90's and was using it until 2019 when I was told that Perl 
is not an approved language in the company and that I have to learn 
Python. So I did. I was a VAX/VMS system admin and instructor from 1987 - 
1996. After 1993 I was working for a SGI distributor, using Irix. I have 
worked with HP-UX, AIX, AT Unix, Wyse Unix, SCO Unix and, eventually, 
Linux, among other things. I might be slightly biased against Windows.

-- 
Mladen Gogala
Database Consultant
https://dbwhisperer.wordpress.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Issues with running python in Command prompt

2021-01-19 Thread Mladen Gogala via Python-list
On Sat, 16 Jan 2021 19:00:06 +0330, mohsen shooshtari wrote:

> hello,
> Thanks in advance for your consideration. I install python3.8 and then
> install Pycharm but when I call python in Command prompt, followed by (
> 'python'
> is not recognized as an internal or external command, operable program
> or batch file.
> 
> what should I do to fix this problem?

Make sure that OS recognizes it as a command. That usually means putting 
the directory where your Python resides into the path. I generally advise
using Cygwin and installing the Cygwin version of Python. Your OS will 
look like a POSIX compatible system, and you will be able to use Unix/
Linux tools like bash, less. vi, awk, grep and alike. You will also be 
able to use "/" as a directory separator which is really helpful when it 
comes to regular expressions.
Another option is to install Ubuntu on top of your Windows 10. I am no 
fan of Ubuntu but it's still much better than Windows. All scripting 
languages (PHP, Perl, Python, Go and JavaScript to mention only the most 
popular ones) are all written with Linux/Unix in mind. There are Windows 
ports but using the Linux or Cygwin version is typically much easier and 
more natural.

-- 
Mladen Gogala
Database Consultant
https://dbwhisperer.wordpress.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: open sentinel-2image python

2021-01-19 Thread Mladen Gogala via Python-list
On Sun, 17 Jan 2021 18:01:26 +, MRAB wrote:

> On 2021-01-17 13:57, Karsten Hilbert wrote:
>> Am Sun, Jan 17, 2021 at 02:20:24AM -0800 schrieb omid mohammadi:
>> 
>>> When I open the sentinel-2 image in Python, I get the following error:
>>>
>>> MemoryError: Unable to allocate 115. MiB for an array with shape
>>> (5490, 5490) and data type float32
>>>
>>> How can I fix it?
>> 
>> You can install more RAM.
>> 
> Or maybe the OP is running 32-bit Python but the code needs >2GB. If
> that's the case then using 64-bit Python might fix it, assuming that
> it's a 64-bit machine.

Or there may be an OS limitation on the size of the address space. I 
would check the OS log first, then do "ulimit -a". If there are sar or
vmstat on the system, I would definitely use them.
Running out of memory is an OS error and should be checked on the OS 
level.



-- 
Mladen Gogala
Database Consultant
http://mgogala.byethost5.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python not Running

2021-01-18 Thread Mladen Gogala via Python-list
On Sat, 16 Jan 2021 14:55:26 -0600, Logan Cranford wrote:

> I downloaded Python but when I try to run Idle it says it is not found
> and I should try to redownload it. When I try to do that all that comes
> up is a page that says modify, repair or uninstall. I have repaired
> several times but it still gives me the same message. Can anyone  help
> me with this?

Have you tried with Perl? What is your OS and how are you trying to run
it? I would suggest doing "sudo bash" and on the root prompt do this:

dnf -y install python3

When you type "which python", your OS should tell you where your Python
is:

[mgogala@umajor tmp]$ which python
/usr/bin/python

You can further test by echoing Python version:


[mgogala@umajor tmp]$ python -V
Python 3.9.1


If you see something like C:\ prompt, then install Cygwin and use your 
Python from Cygwin. Your goal is have bash and vi editor. If that is an
option, reformat your boot drive and install Ubuntu, Mint or Fedora.

[mgogala@umajor tmp]$ cat /etc/redhat-release
Fedora release 33 (Thirty Three)
[mgogala@umajor tmp]$ 

Visual studio code is available both on Linux and Windows and is freaking 
awesome. No need to run Idle. Idle is a flint ax when compared to VSCode. 
VSCode supports auto-completion, formatting and many other things that 
you cannot even dream of in Idle.

PS:
Since you're most probably using Winduhs, use regedit and modify your PATH
variable. Your python will be in c:\Program Files if you installed 64 bit 
version, which you should have done. Otherwise, it will be in C:\Program 
Files(x86). Your "idle" program will be in your "start menu" which came 
back in Winduhs 10. You would probably never have guessed, but I'm not 
particularly fond of Winduhs.



-- 
Mladen Gogala
Database Consultant
http://mgogala.byethost5.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is there a log file that tracks every statement that is being executed when a program is running?

2020-10-25 Thread Mladen Gogala via Python-list
On Sun, 25 Oct 2020 12:14:52 +0100, Maxime S wrote:

> Hi,
> 
> You can use the trace module for that:
> https://docs.python.org/3.8/library/trace.html
> 
> Personally I tend to put print statement at strategic places instead, I
> find that easier to analyse than a full trace but YMMV.
> 
> Maxime

Thank you. I am a YAPN (yet another Python newbie) and this helps me a 
lot.

-- 
Mladen Gogala
Database Consultant
http://mgogala.byethost5.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Simple question - end a raw string with a single backslash ?

2020-10-19 Thread Mladen Gogala via Python-list
On Mon, 19 Oct 2020 02:44:25 +, Stefan Ram wrote:

> Mladen Gogala  writes:
>>In Perl, there are no classes. 
> 
>   If there are no classes in Perl, then what does
> 
> bless REF,CLASSNAME 
> 
>   do?

bless \$ref will make the given reference a reference to the class. And classes 
is Perl
are called "modules". However, Perl classes are not the classes in the  real 
sense. There 
is no inheritance. You can have a similar thing in C: it's called "struct", it 
can do 
similar things like Perl modules. But C isn't object oriented. Inheritance is an
essential characteristic of object orientation.
There were attempts to turn Perl into OO language with Moose 
(https://metacpan.org/pod/Moose)
However, Moose is complex, tedious and rarely used. Perl6 is an OO language, 
but nobody uses 
it. The problem is CPAN and all the Perl5 modules which are not compatible with 
Perl6 or "Roku"
as it is known.



-- 
Mladen Gogala
Database Consultant
http://mgogala.byethost5.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Simple question - end a raw string with a single backslash ?

2020-10-18 Thread Mladen Gogala via Python-list
On Sun, 18 Oct 2020 12:19:18 -0600, Michael Torrie wrote:

> Python certainly is procedural.  A script starts at the top and executes
> through to the bottom and ends, barring any flow control in the middle.
>  Like Perl you can use it in many different ways and paradigms including
> OO if you desire.

That's not the point. In Python, regular expressions are a class. So are 
strings and lists. 
In Perl, there are no classes. Procedural paradigm means that you are modelling 
the real world
with the objects provided by the computer environment: strings, numbers, lists 
or hashes (known 
as "dictionaries" in Python). OO paradigm allows you to create objects and make 
them behave like 
in the real world. In other words, in OO paradighm, data and functions are 
related. The basic 
idea of the OO was to make code sharing much easier. However, things  do not 
always work as 
they should:

https://medium.com/better-programming/object-oriented-programming-the-trillion-dollar-disaster-92a4b666c7c7

https://towardsdatascience.com/object-oriented-programming-is-dead-wait-really-db1f1f05cc44

Of course, not everybody agrees. People never do:

https://techbeacon.com/app-dev-testing/object-oriented-programming-dead-not-long-shot

Sometimes, OO has its funny side:
https://www.wearethemighty.com/articles/that-time-the-australian-air-force-squared-off-against-missile-shooting-kangaroos

-- 
Mladen Gogala
Database Consultant
http://mgogala.byethost5.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Simple question - end a raw string with a single backslash ?

2020-10-18 Thread Mladen Gogala via Python-list
On Sun, 18 Oct 2020 16:13:16 +0200, Peter J. Holzer wrote:

> 
> Ah, I see, that the sillyness of Perl's grammar-altering modules (which
> let you write Perl in Latin (with proper declensions and conjugations,
> of course) or Chinese) has found its way to Python 
>

To tell the truth, I only installed it to test whether it works. I am an
old Perl hack forced to learn Python by the market forces. I learned Perl 
in 1994 which means that it is my first love of a sort. The fundamental 
difference between the two languages is that Perl is procedural while 
Python is a fully OO language. Discussion of Perl vs Python necessarily
devolves into the discussion of procedural vs OO paradigms. And that has 
been decided long time ago, with the advent of Java over COBOL. Market
has spoken and I had to learn Python. So, I stopped worrying and learned 
to love Python. Any resemblance to the subtitle of "Dr. Strangelove" is 
purely accidental.
BTW, Visual Studio Code uses pylint to evaluate my adherence to PEP8 and
there is a "kite" extension with autocompletion. Nothing like that was ever
available for Perl. It certainly is easier to write Python scripts than to
write Perl scripts. I do sort of miss $_, @_ and $!. Python's argparse 
is supreme and much better than Getopt::Long.



-- 
Mladen Gogala
Database Consultant
http://mgogala.byethost5.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: File Name issue

2020-10-18 Thread Mladen Gogala via Python-list
On Sun, 18 Oct 2020 21:00:18 +1300, dn wrote:

> On 18/10/2020 12:58, Mladen Gogala via Python-list wrote:
>> On Sat, 17 Oct 2020 22:51:11 +, Mladen Gogala wrote:
>>> On Sat, 17 Oct 2020 18:12:16 -0400, Steve wrote:
>>>
>>>> with open("HOURLYLOG.txt", 'r') as infile:
>>>> works but, when I rename the file, the line:
>>>> with open("HOURLY-LOG.txt", 'r') as infile:
>>>> does not.  The complaint is: Cannot Assign to operator
> 
>  >>> with open( "HOURLY-LOG.txt", "r" ) as f:
> ... print( f.read() )
> ...
> yes
> ^^^ the entire contents of a magnificent test-file
> 
> NB:
> Python 3.8.5 (default, Aug 12 2020, 00:00:00)
> [GCC 10.2.1 20200723 (Red Hat 10.2.1-1)] on linux
> 
> 
>> BTW, I used this
>> cp /var/log/syslog ./in-file.log
>> #!/usr/bin/env python3
>> import io
>> with open("in-file.log","r") as infile:
>>  for line in infile:
>>  print(line)
>> I got a different error:
>> Traceback (most recent call last):
>>File "./test.py", line 4, in 
>>  for line in infile:
>>File "/usr/lib/python3.8/codecs.py", line 322, in decode
>>  (result, consumed) = self._buffer_decode(data, self.errors, final)
>> UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd8 in position 
897:
>> invalid continuation byte
> 
> 
> @Mladen: is syslog a text file or binary format?

Hi!
Syslog is the system log. It's a text file. This only happens if I use 
infile as iterable. If I use readline, all is well:

#!/usr/bin/env python3
import io
with open("in-file.log","r") as infile:
while True:
line=infile.readline()
if not line:
break
print(line)

I don't particularly like this idiom, but it works. That is probably a bug
in the utf-8 decoder on Ubuntu. It doesn't happen on my Fedora 32 VM. I 
haven't tried with infile.reconfigure(encoding=None)



-- 
Mladen Gogala
Database Consultant
http://mgogala.byethost5.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: MERGE SQL in cx_Oracle executemany

2020-10-18 Thread Mladen Gogala via Python-list
On Sat, 17 Oct 2020 21:23:40 -0600, Jason Friedman wrote:


>> I'm looking to insert values into an oracle table (my_table) using the
>> query below. The insert query works when the PROJECT is not NULL/empty
>> (""). However when PROJECT is an empty string(''), the query creates a
>> new duplicate row every time the code is executed (with project value
>> populating as null). I would like to modify my query so a new row is
>> not inserted when all column values are matched (including when project
>> code is null).
>> I'm guessing I would need to include a "when matched" statement, but
>> not too sure on how to get this going. Would appreciate help with this,
>> thanks.
>>
>> ```
>> con = cx_Oracle.connect(connstr)
>> cur = con.cursor()
>> rows = [tuple(x) for x in df.values]
>> cur3.executemany('''merge into my_table using dual on (YEAR = :1 and
>> QUARTER = :2 and CODE = :3 and AMOUNT = :4 and DATE = :5 and COMMENTS =
>> :6 and PROJECT = :7)
>> when not matched then insert values (:1, :2, :3, :4, :5, :6, :7)
>> ''',rows)
>> con.commit()
>> cur.close()
>> con.close()
>>
>>
> Perhaps the issue is that NULL is not equal to anything. Oracle provides
> the IS NULL function to determine if a value is NULL.
> 
> Note also you define "cur" but executemany with "cur3".
> 
> And is "rows = [tuple(x) for x in df.values]" what you want? Print it.

Obviously, the "PROJECT" column is causing the issue. NULL in Oracle 
database is never equal to anything. If :7 is NULL, your "not matched" 
condition is satisfied and your MERGE statement will insert a new and 
exciting row. That has nothing to do with Python. The only solution is
"ALTER TABLE my_table modify(project not null)" or 
"ALTER TABLE my_table add constraint project_nn check(project is not null)"  I

If you already have NULL values in the PROJECT column than use "add constraint" 
with NOVALIDATE option. Other than that, allowing NULL values in key columns is
a sign of bad design. Namely, Oracle cannot index NULL columns, so PROJECT IS 
NULL
predicate cannot be resolved by an index.

-- 
Mladen Gogala
Database Consultant
http://mgogala.byethost5.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: File Name issue

2020-10-17 Thread Mladen Gogala via Python-list
On Sat, 17 Oct 2020 22:51:11 +, Mladen Gogala wrote:

> On Sat, 17 Oct 2020 18:12:16 -0400, Steve wrote:
> 
>> with open("HOURLYLOG.txt", 'r') as infile:
>> works but, when I rename the file, the line:
>> with open("HOURLY-LOG.txt", 'r') as infile:
>> does not.  The complaint is: Cannot Assign to operator
> 
> Try this:
> 
> with open("HOURLY\-LOG.txt", 'r') as infile:

BTW, I used this
cp /var/log/syslog ./in-file.log

#!/usr/bin/env python3
import io
with open("in-file.log","r") as infile:
for line in infile:
print(line)

I got a different error:



Traceback (most recent call last):
  File "./test.py", line 4, in 
for line in infile:
  File "/usr/lib/python3.8/codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd8 in position 897: 
invalid continuation byte

However, that doesn't have anything to do with your problem. 

-- 
Mladen Gogala
Database Consultant
http://mgogala.byethost5.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: File Name issue

2020-10-17 Thread Mladen Gogala via Python-list
On Sat, 17 Oct 2020 18:12:16 -0400, Steve wrote:

> with open("HOURLYLOG.txt", 'r') as infile:
> works but, when I rename the file, the line:
> with open("HOURLY-LOG.txt", 'r') as infile:
> does not.  The complaint is: Cannot Assign to operator

Try this:

with open("HOURLY\-LOG.txt", 'r') as infile:


-- 
Mladen Gogala
Database Consultant
http://mgogala.byethost5.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Simple question - end a raw string with a single backslash ?

2020-10-17 Thread Mladen Gogala via Python-list
On Thu, 15 Oct 2020 21:30:15 +, Stefan Ram wrote:

> Tony Flury  writes:
>> >>> a = r'end' + chr(92)
> 
>   Or maybe,
> 
> a = r'''
> end\
> '''[ 1: -1 ]
> 
>   ? The first and the last line are messy, but in the middle,
>   the intended string is clearly visible.


You can use perl module for python. It is installable by pip.
Perl has no problems with handling backslashes.

https://pypi.org/project/perl/

What you need is something like this:

mgogala@umajor:~$ perl -e '$a="abcd";$a=~s/$/\\/; print "$a\n";'
abcd\

Python has a problem with backslashes:

Python 3.8.5 (default, Jul 28 2020, 12:59:40) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> a="abcd"
>>> b=re.sub('$',chr(28),a)
>>> b
'abcd\x1c'

>>> b=re.sub('$',chr(0x41),a)
>>> b
'abcdA'
>>> b=re.sub('$','Z',a)
>>> b
'abcdZ'
>>> b=re.sub('$',chr(0x1C),a)
>>> b
'abcd\x1c'
>>> 

Any other character is shown as it should be, except backslash.





-- 
Mladen Gogala
Database Consultant
http://mgogala.byethost5.com
-- 
https://mail.python.org/mailman/listinfo/python-list