kanye_quote = """My greatest pain\
in life is that I will never be able to see myself perform live"""
I am trying to create a line break but not having any luck. I am using the """ method to allow for multiple lines. It was my understanding that \ would create a new line but when I run the script, I am not getting a new line. Any Help would be terrific in solving this little problem.
If you remove the \ you got a line break. The \ is telling python to ignore the line break. The triple quote method lets you enter string on multiple lines. If you want to enter the string on one line you can use \n to get line breaks.
Related
I have a simple bash script template file that I use to generate new bash scripts from python. Each time I change some values in a variable and then create a new copy of the template from python. After saving the file, I give it executable permission. Let me mention it now that I'm using pycharm as the editor. But when I run this newly generated bash script from the terminal it always gives me the following syntax error:
./job_load_8.sh: line 4: syntax error near unexpected token `('
When I open this newly created bash script file in pycharm and comment all echo statements and save the changes, then the script doesn't throw any errors. And when I go back and uncomment all my previously commented statements, then the script works exactly as expected. Clearly, pycharm is doing some auto-formatting that I'm missing. What is happening here ?
Edit: After many people pointed out that I haven't delved sufficient information, I'm giving more details now. Although, I have solved my issue, I'm mentioning more details in case someone else faces this issue and wants an answer.
Here is the python code that inserts certain lines in the bash script:
# at line number 11, insert a variable GPU_FLAG
lines.insert(11, "gpuFlag = " + str(gpu_flag) + "\r")
# at line number 12, insert a variable NUM_ITER
lines.insert(12, "gpuFlag = " + str(gpu_flag) + "\r")
# get the last set of spot file names from the cohort and save them in a list called spot_file_names separated by commas and add a newline
lines.insert(13, "spotFileNames = [" + ", ".join(["'" + spotFileNames[spot_cohort*(i//spot_cohort)+k] + "'" for k in range(i%spot_cohort+1)]) + "]\r")
And shown below is the result of diff between the two scripts, i.e., before and after commenting and uncommenting.
Answering here even though I have solved my issue in case someone in the future may be in need of a quick fix. As you can see that there are these extra ^M characters in the original which was not there in the fixed bash script. I have no idea where they come from. But once I remove them, my issue is solved. I don't know how to remove them from the python script so now I just insert after every two lines and that solves my issue. Somehow pycharm (practically every other editor) does this automatically when a file is saved after commenting and uncommenting. At least that's what I'm guessing is happening. Nevertheless, my issue is now solved and I've told why. Hope this helps.
My problem is the following. I have a text file with a bunch of lines in it. The problem is this text might have been created by Windows or Unix or Mac.
I want to open this text in python (as a string block) and split according to a break line to get an array at the end with all lines. The problem is I only tested this with a windows created file so I can split the string block easily according \n. But if I understand correctly other environnement use \r \r\n ...Etc
I want a general solution where I can detect what kind of line break is used in a file before I start splitting in order to split it correctly. Is that possible to do?
thanks;
UNIX_NEWLINE = '\n'
WINDOWS_NEWLINE = '\r\n'
MAC_NEWLINE = '\r'
This will be how the different os apply line breaks in a file and how python sees it
I have a function that prints the number of pixels found in an image and then asks the user how they would like to proceed. As long as the interpreter hasn't moved on from the function I want all the output to be indented accordingly.
One such 'sub output' (the input prompt) needs to be multiple lines. So I kick off with the 3*quote (''') followed by two spaces to create the indentation. At the end of the question 'how would you like to proceed?' I use a hard return. An extra indentation is assumed by the text editor so I remove it causing the following list of suggestions to line up flush with the input variable command. Here's how it looks:
def returnColors():
#
# lots of code that does stuff...
#
print("The source image contains", lSize, "px.")
print("")
command=input(''' What would you like to do? You can say:
get all
get unique
''')
The problem with this is that the interpreter is acknowledging the indentation that separates the function body from the function statement as actual string contents, causing the output to look like this:
The source image contains 512 px.
What would you like to do? You can say...
get all
get unique
|
The only way to avoid this is by breaking indentation in the interpreter. Although I know it works, it doesn't look very good. So what options do I have?
EDIT: Just because I have the screenshot_
One thing that you should keep in mind is that once you have start a multiline string declaration, all the text until it is closed is taken as is and syntax (ie, indentation) is no longer considered.
You can start your multiline with an explicit new line so that everything in the multiline string can be indented together in code.
IE.
command=input('''
What would you like to do? You can say:
get all
get unique
''')
would print out the prompt with a new line on top, but the formatting of the text is more explicitly shown and should appear as seen.
OR you could use the \n for each new line in the string to get it formatted more correctly and remember to use a single \ after each new line. E.g.
instead of:
''' What would you like to do? You can say:
get all
get unique
'''
Try
' What would you like to do? You can say:\
\n\
\n get all\
\n get unique\
\n'
The indent won't matter, no matter where you use \n at the beginning of new line, the input() will output the same. This is will give the same input() string:
' What would you like to do? You can say:\
\n\
\n get all\
\n get unique\
\n'
I recently needed to wrap listings in a LaTeX document I was working on in a minipage environment. Essentially, to insert some text before and after each line in a file that matched. To do this I used the following sequence of commands:
:g/lstinputlisting/:norm O\begin{minipage}{\textwidth}
:g/lstinputlisting/:norm o\end{minipage}
While this gave the result I desired it seemed clunky to have to enter two separate commands, both operating on the same matched line. Is it possible to execute multiple commands on the same line or is the repeated command really necessary?
To wrap:
lstinputlisting
with tags:
\begin{minipage}{\textwidth}
lstinputlisting
\end{minipage}
You can use this command:
:%s/lstinputlisting/\\begin{minipage}{\\textwidth}\r&\r\\end{minipage}
\r is new line.
& is search pattern.
One solution I found while writing this was to use in the string passed to the norm command and then using movement commands to insert the second line. This gives the slightly improved:
:g/lstinputlisting/norm O\begin{minipage}{\textwidth}^[jo\end{minipage}
(^[ is produced by pressing ^V (Ctrl+V) followed by the escape key.)
I'm using a raw strings but when I print the string I'm getting extra tabs at the start of each line.
val rawString = """here is some text
and now im on the next line
and this is the thrid line, and we're done"""
println(rawString)
this outputs
here is some text
and now im on the next line
and this is the thrid line, and we're done
I've tried setting different line endings but that had no effect.
I'm working on the Mac (OS X tiger) using jEdit as my editor. I get the same result when I run the script in the scala interpreter or when I write the output to file.
Anybody know whats going on here?
This problem is due to the fact that you are using multiline raw strings in the interpreter. You can see that the width of the extra spaces is exactly the size of the scala> prompt or the size of the pipe + spaces added by the interpreter when you create a new line in order to keep things lined up.
scala> val rawString = """here is some text // new line
| and now im on the next line // scala adds spaces
| and this is the thrid line, and we're done""" // to line things up
// note that the comments would be included in a raw string...
// they are here just to explain what happens
rawString: java.lang.String =
here is some text
and now im on the next line
and this is the thrid line, and we're done
// you can see that the string produced by the interpreter
// is aligned with the previous spacing, but without the pipe
If you write your code in a Scala script and run it as scala filename.scala you do not get the extra tabs.
Alternatively, you can use the following construct in the interpreter:
val rawString = """|here is some text
|and now im on the next line
|and this is the thrid line, and we're done""".stripMargin
println(rawString)
What stripMargin does is strips anything before and including the | character at the start of every line in the raw string.
EDIT: This is a known Scala bug -- thanks extempore :)
UPDATE: Fixed in trunk. Thanks extempore again :).
Hope it helps :)
-- Flaviu Cipcigan