Skip to content

Tag: python

Find Nth item in comma separated list in Python

I have a large CSV with comma separated lines of varying length. Sorting another set of data I used split(‘,’) in a loop to separate fields, but this method requires each line to have the same number of entries. Is there a way I can look at a line and, independent of the total number of entries, j…

Django Management Command Argument

I need to pass in an integer argument to a base command in Django. For instance, if my code is: I want to run: so, it will return 16. Is there a way I can pass an argument through the terminal to the command I want to run? Answer Add this method to your Command class: You can then use

ValueError: Data must not be a string

I am trying to do the following with requests: However, I get the following error: Note that if I remove the files parameter, it works as needed. Why won’t requests allow me to send a json-encoded string for data if files is included? Note that if I change data to be just the normal python dictionary (a…

Python zipfile, How to set the compression level?

Python supports zipping files when zlib is available, ZIP_DEFLATE see: https://docs.python.org/3.4/library/zipfile.html The zip command-line program on Linux supports -1 fastest, -9 best. Is there a way to set the compression level of a zip file created in Python’s zipfile module? Answer Starting from p…

Python regular expression for consonants and vowels

I was trying to create a regular expression which would allow any number of consonants , or any number of vowels , or a mix of consonants and vowels such that we only have any number of consonants in the beginning followed by any number of vowels ONLY, no consonant should be allowed after the vowels, it would…

Django Model MultipleChoice

I know there isn’t MultipleChoiceField for a Model, you can only use it on Forms. Today I face an issue when analyzing a new project related with Multiple Choices. I would like to have a field like a CharField with choices with the option of multiple choice. I solved this issue other times by creating a…

Python: reduce (list of strings) -> string

I expected something like ‘a.b.c.d’ or ‘abcd’. Somehow, can’t explain the results. There are similar questions here, but not quite like this one. Answer The result of executing the function passed as the first parameter, will be the first parameter to that function in the next it…