Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5987691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:51:07+00:00 2026-05-22T22:51:07+00:00

I am working on a simple import routine that translates a text file to

  • 0

I am working on a simple import routine that translates a text file to a json file format for our system in python.

import json

# Open text file for reading
txtFile = open('Boating.Make.txt', 'r')

# Create picklist obj
picklistObj = dict()
picklistObj['name'] = 'Boating.Make'
picklistObj['items'] = list()

i = 0
# Iterate through each make in text file
for line in txtFile:
    picklistItemObj = dict()
    picklistItemObj['value'] = str(i)
    picklistItemObj['text'] = line.strip()
    picklistItemObj['selectable'] = True
    picklistObj['items'].append(picklistItemObj)
    i = i + 1
txtFile.close()

picklistJson = json.dumps(picklistObj, indent=4)
print picklistJson

picklistFile = open('Boating.Make.json', 'w')
picklistFile.write(picklistJson)
picklistFile.close()

My question is, why do I need the “strip”? I thought that python was supposed to magically know the newline constant for whatever environment I am currently in. Am I missing something?

I should clarify that the text file I am reading from is an ASCII file that contains lines of text separated ‘\r\n’.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:51:07+00:00Added an answer on May 22, 2026 at 10:51 pm

    Python keeps the new line characters while enumerating lines. For example, when enumerating a text file such as

    foo
    bar
    

    you get two strings: "foo\n" and "bar\n". If you don’t want the terminal new line characters, you call strip().

    I am not a fan of this behavior by the way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a simple chat application using a System.Windows.Forms.WebBrowser Control to display
I am working on a simple notification service that will be used to deliver
I'm working on a simple tool that transfers files to a hard-coded location with
I'm working on a simple HTML scraper for Hulu in python 2.6 and am
Currently I am working on a python project that contains sub modules and uses
I'm working on simple counter. My problem is that drawString() method draws new string
OK, well I've been working on a simple app that allows users to save
I'm looking for good/working/simple to use PHP code for parsing raw email into parts.
I'm working on a simple ASP.Net page (handler, actually) where I check the value
I'm working on a simple multiplayer game in which 2-4 players are placed at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.