Home > Software engineering >  for python string how can I print each line as a new line?
for python string how can I print each line as a new line?

Time:12-17

I have a weird problem. I call a API and get this result:

1. Increase the use of alternative fuels: The aviation industry can reduce its carbon emissions by increasing the use of alternative fuels such as biofuels, hydrogen, and synthetic fuels.
2. Improve aircraft efficiency: The aviation industry can reduce its carbon emissions by improving the efficiency of aircraft through the use of advanced materials, aerodynamic designs, and lighter engines.
3. Utilize air traffic management systems: The aviation industry can reduce its carbon emissions by utilizing air traffic management systems that optimize flight paths and reduce fuel consumption.
4. Invest in research and development: The aviation industry can reduce its carbon emissions by investing in research and development of new technologies that can reduce emissions.
5. Increase the use of renewable energy: The aviation industry can reduce its carbon emissions by increasing the use of renewable energy sources such as solar, wind, and geothermal.
6. Implement carbon offset programs: The aviation industry can reduce its carbon emissions by implementing carbon offset programs that allow airlines to purchase carbon credits to offset their emissions.

I am trying to print each line as it's own line(I want to save this to variable later) but it's not working. When I try:

for item in reponse:
  print("*")
  print(item)

it just prints 1 character at a time. What can I do to save each line at a time? I was trying to see the raw string data but I'm not sure how or why it's doing a new line.

What can I do?

CodePudding user response:

It seems your API is returning to you a single string with multiple lines. There are numerous ways to handle this. One is to use the splitlines() method on the result, so something like:

for item in response.splitlines():
    print(item) # which is a single line

You may also have some strange end-of-line characters depending on whether the formatted response text is for Windows, Mac, or Linux.

Also keep in mind that splitlines() includes an option of including or excluding the new line character at the end of each line.

Here's from the Python reference:

str.splitlines(keepends=False) 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.

This method splits on the following line boundaries. In particular, the boundaries are a superset of universal newlines.

Representation

Description

\n

Line Feed

\r

Carriage Return

\r\n

Carriage Return Line Feed

\v or \x0b

Line Tabulation

\f or \x0c

Form Feed

\x1c

File Separator

\x1d

Group Separator

\x1e

Record Separator

\x85

Next Line (C1 Control Code)

\u2028

Line Separator

\u2029

Paragraph Separator

Changed in version 3.2: \v and \f added to list of line boundaries.

For example:

'ab c\n\nde fg\rkl\r\n'.splitlines() ['ab c', '', 'de fg', 'kl'] 'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True) ['ab c\n', '\n', 'de fg\r', 'kl\r\n'] Unlike split() when a delimiter string sep is given, this method returns an empty list for the empty string, and a terminal line break does not result in an extra line:

"".splitlines() [] "One line\n".splitlines() ['One line'] For comparison, split('\n') gives:

''.split('\n') [''] 'Two lines\n'.split('\n') ['Two lines', '']

CodePudding user response:

It must be that response is one large string containing all the lines, so when you iterate over it, you get one character at a time.

Strings have a splitlines() method that will split a large string into separate lines, based on newline characters.

Try this instead:

for item in response.splitlines():

CodePudding user response:

What you can do, not the best solution, is making all one string, and then using /n at the end of each line, for example:

Code:

string = "Hello World/nIts a great day" print(string)

Output:

Hello World Its a great day

The \n character to create a new line within a single string. This is known as a line break.

  • Related