Python Notes etc.

Posted 2022-04-16 4:08 by azri.

Python String manipulation
JSON & SQL
Forming SQL Queries from JSON
JSON Structure
Pretty printing
Boolean and limitations
Textmate & Regular Expressions
Exiting if clause
nano, less & textmate
has_key or in 
You could use :%s/. \{1}$// to delete 1 character off the end of each line.


python_map = { "foo": "bar", [ "baz", "biz" ] } sql = "INSERT INTO your_table (json_column_name) VALUES (%s)" cursor.execute( sql, (json.dumps(python_map),) )

https://stackoverflow.com/questions/4251124/inserting-json-into-mysql-using-python
https://stackoverflow.com/questions/36593638/python-list-comprehensions-join-with-for-loop
https://stackoverflow.com/questions/1388818/how-can-i-compare-two-lists-in-python-and-return-matches
https://stackoverflow.com/questions/8249539/python-how-to-concatenate-to-a-string-in-a-for-loop
https://stackoverflow.com/questions/4251124/inserting-json-into-mysql-using-python
https://towardsdatascience.com/how-to-best-work-with-json-in-python-2c8989ff0390
https://stackoverflow.com/questions/2069662/how-to-exit-an-if-clause
https://stackoverflow.com/questions/1323410/should-i-use-has-key-or-in-on-python-dicts
https://flexiple.com/check-if-key-exists-in-dictionary-python
https://stackoverflow.com/questions/11303032/how-to-add-text-at-the-end-of-each-line-in-vim
https://stackoverflow.com/questions/6118948/bash-loop-ping-successful
https://itsfoss.com/recover-deleted-files-linux/
https://stackoverflow.com/questions/28421864/how-do-i-remove-first-5-characters-in-each-line-in-a-text-file-using-vi

((count = 60))                           # Maximum number to try.
while [[ $count -ne 0 ]] ; do
    ping -c 1 8.8.8.8                    # Try once.
    rc=$?
    if [[ $rc -eq 0 ]] ; then
        ((count = 1))                    # If okay, flag loop exit.
    else
        sleep 1                          # Minimise network storm.
    fi
    ((count = count - 1))                # So we don't go forever.
done

if [[ $rc -eq 0 ]] ; then                # Make final determination.
    echo `say The internet is back up.`
else
    echo `say Timeout.`
fi



 

This will do it to every line in the file:

:%s/$/,/

If you want to do a subset of lines instead of the whole file, you can specify them in place of the %.

One way is to do a visual select and then type the :. It will fill in :'<,'> for you, then you type the rest of it (Notice you only need to add s/$/,/)

:'<,'>s/$/,/



Use str. replace() to replace a string in a list
  1. strings = ["a", "ab", "aa", "c"]
  2. new_strings = []
  3. for string in strings:
  4. new_string = string. replace("a", "1") Modify old string.
  5. new_strings. append(new_string) Add new string to list.
  6. print(new_strings)