What's happened in endswith function in Python? - python-2.7

I have a file,namedtest.txt
182.7 100.0
182.6 100.0
182.8 100.0
I want to sure weather the line ends with the digits 100.0:
So I use the following code:
for line in open('test.txt','rt'):
print repr(line),line.endswith('100.0\n')
print
But the Output in Ubuntu is:
'182.7 100.0\r\n' False
'182.6 100.0\r\n' False
'182.8 100.0' False
But the Output in windows server 2008 is:
'182.7 100.0\n' True
'182.6 100.0\n' True
'182.8 100.0' False
I already using rt in open function,so why there is difference between different systems yet?

The function strip() can take care of the ending new line character, and it is independent of the operating system used, the code can be
print "{line} {expr}".format(line=repr(line), expr=line.rstrip().endswith('100.0'))

Thanks! Is the answer obvious now? The file was created on Windows, where lines end with "\r\n", not with "\n". So long as you read the file on Windows in text mode, Python can hide that from you. But you read the file on Linux, where the distinction between "text" and "binary" modes doesn't exist, and you get exactly whatever bytes are in the file.
My advice is to force line ends to the native convention on whatever platform you move files to. Short of that, in Python 2 you can open files in "universal newline mode" instead, as briefly explained here.

Related

How to output new line in Visual Studio actions?

I add a break point in Visual Studio 2015, with an action to output a string to Output Window. There will be an auto-added line break at the end. Problem is, my previous output message(which is not output by break point) has no line break.
So I want to add new line character at the beginning of my string, to avoid it messing up with my previous message. I tried to add \n, but the \n outputs as it is, without being escaped.
How to add a new line character in break point's action?
Here are four things for you to try:
You can produce a line break using the debugger expression {"\n",s8b} which makes use of the C++ debugger format specifier s8b (unquoted 8-bit string).
Here's an example with a two-line message First{"\n",s8b}Second:
(Other than that, I am not aware of any other way to include line breaks in the message. While there are ways to enter a multi-line message (by entering line break characters' Unicode code points using the numpad), Visual Studio will just throw away everything but the first text line entered.)
Just before your current breakpoint, add an additional breakpoint with a very short action message (a dot or comma) in order to get an additional line break before your real message.
If you're on Windows (which appears likely, given Visual Studio), you can send a message to the debugger using the Windows API function OutputDebugString. This is the currently suggested solution to the SO question, "How do I print to the debug output window in a Win32 app?"
Write a message to clog: std::clog << message << std::endl;.
In Addition to the answer from stakx that matches the original question for debugging C++ applications, I would like to add a character sequence that instead works for debugging .NET applications:
{"\n",nq}
The C++ sequence would otherwise result in this error message: 's8b' is not a valid format specifier

Python - encoding/decoding Windows vs Linux

Having this line of code:
print "S\x00A\x00V\x00A"
produces different output. On Windows:
S A V A
and on Linux:
SAVA
What is the difference between the the 2 platform and what can I do to remove the whitespaces from the Windows case?
The difference is at the terminal level.
Windows cmd prints the zero-char as empty whereas your Linux terminal doesn't print it.
Note that in PyScripter console or PyCrust console (wx-based) you only get S (probably because the zero is seen as a line-termination char). So it's definitely not portable :)
To get rid of it just perform a replace:
print("S\x00A\x00V\x00A".replace("\x00",""))

How to print in the same position in fortran

Is it possible to replace a character in terminal with another character by changing format of print command or at least clear a line in terminal?
It is not possible in standard Fortran. Perhaps try using the GNU Readline library. Related: Autocomplete from directory in Fortran
If you do not use the Windows terminal you can use the ANSI escape codes. For example, this first prints the stars, than moves back to the same line and writes "test" over the stars:
print *, "***********"
print *,achar(27)//"[2A"
print *,"test"
end
Tested on a Linux terminal, will not work in the basic Windows terminal.

Pre-assign parameter in script

Actually I have trouble naming the title of this post. Because I don't know how to summarize my meaning in a professional way. But I'll explain my question as below:
I'm running a program written by C++, command is:
./VariationHunter_SC
Then it'll let you type in many parameters:
Please enter the minimum paired-end insert size:
Please enter the maximum paired-end insert size:
Please enter the pre-processing mapping prune probability:
Please enter the name of the input file:
Please enter the minimum support for a cluster:
Obviously I need to type in such parameters one by one to run the program; But I have thousands of such jobs, and need to pre-assign such parameters in script, and submit script to computer.
So how can I do that?
thx
Edit
so how can I make parameter-list?
Just like below?:
140
160
0
mrfast.vh
1
Seems the program cannot recognize these numbers, and distribute numbers..
This depends on how the program actually reads the data that you type in - it's likely that its reading stdin, so you could use separate files with the parameters and pass them in via redirection: ./VariationHunter_SC < parameter-file
It's also possible that the program will accept parameters on the command line, but there's no way of really knowing that (or how) except by whatever documentation the program might come with (or by reading the source code, if it's available and there is no other accurate docs).
Simply use the piping character to pipe the contents of a file to your program
example, in a windows command shell:
echo "asdf" | pause
This will pass "asdf" to the pause program. As a result, pause will print a "Press any key to continue" message, then imediately continue because it will receive the "asdf" string as a response.
So, overall, write or use a program that outputs the contents of your file. Call it, then pipe its output to the program that needs the input.
The unix cat command is such a command that writes the contents of a file to output, or to the input of another executable if you are piping the output.

popen and system behaves unexpectedly with multiple quoted file paths

I am trying to execute a dos command from within my C++ program, however soon as I add quotes to the output filepath (of a redirection) the command no longer gets executed and returns instantly. I've shown an example below of a path without spaces, but since paths may have spaces and thus be quoted for the shell to understand it properly I need to solve this dilemma - and I'm trying to get the simplest case working first.
i.e.
The following WORKS:
sprintf(exec_cmd,"\"C:/MySQL Server 5.5/bin/mysqldump.exe\" -u%s -p%s %s > C:/backup.bak",user,password,db_name);
system(exec_cmd);
The following does NOT work (notice the quotes around the output):
sprintf(exec_cmd,"\"C:/MySQL Server 5.5/bin/mysqldump.exe\" -u%s -p%s %s > \"C:/backup.bak\"",user,password,db_name);
system(exec_cmd);
I'm guessing it is choking somewhere. I've tried the same "exec_cmd" in popen to no avail.
Any help/advice is greatly appreciated.
I don't think your shell (cmd.exe) allows redirection to a file name with spaces. I couldn't make my command.com from DOS 6.22 accept it (I don't have a cmd.exe nearby to test).
Anyway, you can use the --result-file option to pass the redirection to the command itself.
mysqldump ... --result-file="file name" ...