views:

44

answers:

1

i have tested code in firefox under ubuntu:

the frontend is a textarea,in textarea press the key ENTER,then submit to the server,

on the backend you'll get find \r\n string

r=request.POST.get("t")
r.find("\r\n")>-1:
    print "has \r\n"

my question is when we will get \r\n ,when we'll get \n?is this platform independent?

this is important when want to use this string to use as a regular expression,any adivse is welcome

A: 

Yes, you are correct, you are dealing with a platform-specific ways to encode a newline:

  • In Windows platforms, a newline is typically encoded as \r\n
  • In Linux/Unix/OS X, a newline is typically encoded as \n

Similarly, web browsers tend to favor these conventions: IE uses \r\n newlining, whereas Safari and Firefox use \n.

As a solution, considering using Python functions that are aware of different new line encodings, e.g. provide a universal newline support.

For instance if you want to split a string into lines, use splitlines:

lines = r.splitlines()
jsalonen