Detecting line breaks within Python/Django textarea fields - python

I have a textarea field in Django from which I want to detect line breaks. I can see that the line breaks are stored as reloading the page with the user input shows the breaks that the user inputted, but I can't detect them in Django/Python.
When I look at the output from PostgreSQL I can see \r characters, but these don't seem to show up within the Python environment.
I'd like to do something like this:
text_blocks = text_area.split("\r")
But this doesn't work and I suspect is naive.
How can I detect these seemingly invisible line breaks within Python/Django?

Try splitlines(), this is a built-in string method of python:
text_blocks = text_area.splitlines()
From the docs:
Return a list of the lines in the string, breaking at line boundaries.
Line breaks are not included in the resulting list unless keepends is
given and true.

Related

vscode python avoid indent of multiline string

So here is an example of a multiline string in vscode/python:
Cursor is after the p , and then you press enter, and end up like this:
i.e. the string ends up indented, which seems what you almost never want - why have an arbitratly amount of whitespace on the next line of this string ?
Is there any way change this in vscode, i.e. for multiline strings, it should end up with this:
I think this problem is related to different coding styles of different people.
For example,
def example(x):
if x:
a = '''
This is help
'''
def example(x):
if x:
a = '''This is help
'''
The automatic indenting of vscode line breaks is based on code blocks. If you want Vscode can identify multiline string, I think it would be better to submit future request in github. I've submitted this issue for you.
I am not 100% sure if what OP meant is just to refer to the indentation in the editor (namely, VSC) or if, by this:
i.e. the string ends up indented, which seems what you almost never want - why have an arbitrary amount of white space on the next line of this string?
...they also meant to refer to the actual output of the multi-line string,
(or also, just in case anybody else finds this post looking for a way to avoid this affecting the actual output of the multi-line string), I'd like to add as a complementary answer (cannot comment yet) that this was already beautifully answered here.
If that's the case and you're reading this for that reason, in short, all you want is to import the standard lib 'inspect' and post-process your string with it, using the cleandoc method.
Without breaking the indentation in your IDE, this method makes sure to give you the string output you actually expected:
All leading whitespace is removed from the first line. Any leading whitespace that can be uniformly removed from the second line onwards is removed. Empty lines at the beginning and end are subsequently removed. Also, all tabs are expanded to spaces.
(From the docs link above)
Hope that helps anyone.

Find string in HTML file and return adjacent value using Python

I have an HTML file which has a bunch of fields, among which a specific string appears followed by a value within parenthesis. For example: RECORD_DURATION (0:00:15). The string RECORD_DURATION will occur multiple times throughout the HTML file. I want to write a script which will search for the RECORD_DURATION string and return the value within the parenthesis next to the string. So I'm anticipating my output to look something like this:
0:00:15
Or if there are multiple durations (which is almost always the case), then return a list containing those values
[0:00:15, 0:00:08, 0:00:11]
The code I have so far only attempts to read the entire file and return a line which has the string in it:
with open("filename.html", 'r') as searchfile:
for line in searchfile:
if 'RECORD_DURATION' in line:
print line
However, the entire file is in a line so it ends up returning everything. Is there a way to do this cleverly in Python?
Probably the simplest is the following: before, after = line.split('RECORD_DURATION',1)
This is gonna get unwieldy quickly tho, so either use look at the "re" module, or better, the "beautifulsoup" module.

Macro to get document contents preserving hyphenation in libreoffice writer

I need to access the text in a LibreOffice document.
The document has automatic hyphenation,
and I need to know the hyphen positions as they are displayed on screen.
The following code returns clear text without automatic hyphens:
XSCRIPTCONTEXT.getDocument().getText().getString()
This is the documentation I read:
https://wiki.openoffice.org/wiki/Documentation/DevGuide/Text/Working_with_Text_Documents
Also I looked at this extension: https://github.com/voikko/libreoffice-voikko
I also ran the Capitalise.py example under pyCharm remote debugger, but couldn't find any hints.
Automatic hyphens do not actually occur in the text in LibreOffice. Instead, they are displayed as needed. When a format such as PDF is exported, or if the document is printed, then hyphens are shown in the output.
The Hyphenator service is fairly easy to use in macros, and allows a word to be split up according to possible hyphenation positions.
To really determine where hyphens are getting displayed on screen, the following may work:
Traverse the document with a word cursor. Andrew Pitonyak's Macro Document section 7.3.8.5 gives an example of this in Basic.
Move the view cursor to the beginning of each word and check the Y position. For example, if self.oVC is the view cursor, then check the value of self.oVC.getPosition().Y.
Move the cursor to the end of the word, and see if the Y position changed.
If it did, then presumably the word was hyphenated.

Cannot post Python code to the website rosalind.info

I am trying to post a sample solution, written in Python, to rosalind.info.
I tried following their instructions:
To highlight code, add a shebang styled first line :::lexername to your code. Replace lexername with the lexer keyword for the language that you want to be highlighted as shown in the list of Pygments lexers."
However, I can't get it to work.
I have tried setting the first line to:
:::python
:::PythonLexer
#!:::python
#!:::PythonLexer
but it just appears as ordinary text.
It seems your first attempt was correct, but you did not click the 'Submit' button to view your code with the lexer applied.
In order to see the code with syntax highlighting, you must first submit your response. The WYSIWYG editor provided below the markdown box does not perform syntax highlighting. In order to see your code with proper highlighting you would type something like the following into the box.
:::python
print "Hello World"
which will look something like
print "Hello World"
once you click the 'Submit' button and view your response. You will have the option to edit your submission if you want to change things later.
Joshua's answer has linked you to the place where you can determine which lexer name you want to use. Simply choose type the corresponding 'short name' for the highlighting you would like to apply.
You could try
#!:::python3
Source: http://pygments.org/docs/lexers/
Two ways that worked for me were:
Indent the whole code block with 4 spaces. Make the first indented line either
#! python or
:::python.
The first version also adds line numbers to the formatted code.
Instead, you can also surround the code block with lines containing only triple backticks ```, in which case you don't need to indent the code with spaces. As in the previous case, make the first line after the opening backtick line either
#! python or
:::python.
The first version adds line numbers, as mentioned above.
As mentioned before by others, you need to "Submit" before you see the fully formatted result.

Extra line breaks being displayed after SQL storage

I'm currently developing this in Python (with web.py) on Windows, and using latest Chrome.
Simple test:
User is shown a basic web form with a component.
When form is submitted, the content of this textarea is placed into a MySql table, unmodified.
Later, the user returns to edit their last submission.
I then present a new form, with the textarea populated directly from the database for modification - HTML is prevented from being processed so tags are displayed.
However, when re-displayed to the user, every line now has an extra (unwanted) line-break between each line.
How can I prevent this?
eg:
Submitted Text:
Line 1
Line 2
When re-displayed, the text looks like:
Line 1
Line 2
I'm aware that this is going to be some kind of CR LF issue but can't quite get to the solution.
I tried a conversion to <br /> but that just displays the <br /> text not an actual line break.
I don't really want to modify the text before putting it into the database either.
But I guess I do need something that would compensate for various OS that display line breaks differently.
I've read through many of the similar questions here, but they are primarily PHP, or talk about nl2br which wouldn't be a solution here anyway.
If you are using print to output the text, append a comma at the end of your statement to remove the new-line character.
e.g.
print 'Some Text',
It may be that a new-line is already in your printed text and doesn't require the extra appended one from print.
If not, try .rstrip('\n') on your string to remove any new-lines.

Categories

Resources