1

I am using python to run a loop to read an array and print the results. I can get the results to print but they come back like this. IWBBPB00210IWBBPA00065. When it should look like this

IWBBPB00210
IWBBPA00065

Here is my code.

with open('Count_BB_Serial_weekly.json', 'r') as lowfile:
  low = json.load(lowfile)

low1 = low["total_serials"]
low3 = ""

low2 = low["serials"]
for i in range(len(low2)):
    #print(low2[i])
    low3 += low2[i]

print(low3)

When I originally printed low2[i] it would come back how I want but later on I have to send this in a message and in the message it would only send the last value from low2. I have tried adding '\n' to the end of low3 but this does not work.

How do I properly add this new line feed. Thanks for the help!

1
  • 1
    The newline character is '\n'. Commented Jul 20, 2017 at 3:16

3 Answers 3

4

The proper way to loop through a list is:

for i in low2:
   low3 += i 

A still more Pythonic way is not to loop at all:

low3 = '\n'.join(low2)

The latter incidentally solves your newline problem.

Sign up to request clarification or add additional context in comments.

Comments

0

The print() function appends newline by default. To construct the string by yourself, you can append the \n character to the string:

for i in range(len(low2)):
    #print(low2[i])
    low3 += low2[i] + '\n'

print(low3)

But as long as you have all the lines in the list low2, you can simply join it with the \n char:

'\n'.join(low2)

Comments

0

I'm not sure if I understand your question. But I believe you want a line break. You can archive that by adding '\n'

with open('Count_BB_Serial_weekly.json', 'r') as lowfile:
  low = json.load(lowfile)

  low1 = low["total_serials"]
  low3 = ""

  low2 = low["serials"]
  for i in range(len(low2)):
    #print(low2[i])
    low3 += low2[i] + '\n'

print(low3)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.