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 982165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:39:14+00:00 2026-05-16T04:39:14+00:00

I would like to write a list to a file and read back the

  • 0

I would like to write a list to a file and read back the contents of the file into a list.
I am able to write the list to the file using simplejson as follows:

f = open("data.txt","w")
l = ["a","b","c"]
simplejson.dump(l,f)
f.close()

Now to read the file back i do

file_contents = simplejson.load(f)

But, i guess file_contents is in json format. Is there any way to convert it to a list ?

Thank You.

  • 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-16T04:39:15+00:00Added an answer on May 16, 2026 at 4:39 am
    with open("data.txt") as f:
      filecontents = simplejson.load(f)
    

    is indeed reloading the data exactly as you specify. What may be confusing you is that all strings in JSON are always Unicode — JSON (like Javascript) doesn’t have a “byte string” data type distinct from “unicode”.

    Edit I don’t have the old simplejson around any more (since its current version has become part of the standard Python library as json), but here’s how it works (making json masquerade as simplejson in the hope of avoiding confusing you!-)…:

    >>> import json
    >>> simplejson = json
    >>> f = open("data.txt","w")
    >>> l = ["a","b","c"]
    >>> simplejson.dump(l,f)
    >>> f.close()
    >>> with open("data.txt") as f: fc = simplejson.load(f)
    ... 
    >>> fc
    [u'a', u'b', u'c']
    >>> fc.append("d")
    >>> fc
    [u'a', u'b', u'c', 'd']
    >>> 
    

    If this exact code (net of the first two lines if what you do instead is import simplejson of course;-) doesn’t match what you observe, you’ve found a bug, so it’s crucial to report what versions of Python and simplejson you’re using and exactly what error you get, complete with traceback (edit your Q to add this — obviously crucial — info!).

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

Sidebar

Related Questions

I would like to write an address in a text file, which can be
I would like to read each line of text from the file outputted from
I'm trying to write an array (list?) to a text file using Python 3.
I have a problem with using streams. I would like to read my html
I would like to make qqplots through a list of dataframes. I wrote this
I would like to write an abstract base class class func_double_double_t : public unary_function<double,
I would like to write a function that creates a plot of a set
I would like to write an if statement that would continue to repeat a
I would like to write a query that returns a single date value, that
I would like to write a League Fixture generator in python, but I can't.

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.