ara
ara

Reputation: 39

How to read a file in python which has newline and tabs into a string?

I am trying to read a file which has tabs and newline etc and the data is JSON format.

When I read it using file.read()/readlines() etc, all the newlines and tabs are also read.

I have tried rstrip(), split etc but in vain, maybe I am missing some thing:

Here is essentially what I am doing:

 f = open('/path/to/file.txt')
 line = f.readlines()
 line.split('\n')

This is the data (including the raw tabs, hence the poor formatting):

        {
      "foo": [ {
       "id1" : "1",
   "blah": "blah blah",
       "id2" : "5885221122",
      "bar" : [
              {  
         "name" : "Joe JJ", 
          "info": [                 {
         "custid": "SSN",    
         "type" : "String",             }        ]
        }     ]     }     ]  }

I was wondering if we can ignore it elegantly.

Also hoping to use json.dumps()

Upvotes: 3

Views: 9814

Answers (6)

user539810
user539810

Reputation:

$ cat foo.json | python -mjson.tool
Expecting property name: line 11 column 41

The comma in "type" : "String", is causing the JSON decoder to choke. If it wasn't for that problem, you could use json.load() to load the file directly.

In other words, you have malformed JSON, meaning you'll need to perform a replacement operation before feeding it to json.loads(). Since you'll need to read the file into a string completely to do the replacement operation anyway, use json.loads(jsonstr) instead of json.load(jsonfilep):

    >>> import json, re
    >>> jsonfilep = open('foo.json')
    >>> jsonstr = re.sub(r'''(["'0-9.]\s*),\s*}''', r'\1}', jsonfilep.read())
    >>> jsonobj = json.loads(jsonstr)
    >>> jsonstr = json.dumps(jsonobj)
    >>> print(jsonstr)
    {"foo": [{"blah": "blah blah", "id2": "5885221122", "bar": [{"info":
    [{"type": "String", "custid": "SSN"}], "name": "Joe JJ"}], "id1": "1"}]}

I only used the re module because it could happen for any value, number or string.

Upvotes: 0

liliumdev
liliumdev

Reputation: 1179

A little hack, inefficient I guess:

f = open("/path/to/file.txt")
lines = f.read().replace("\n", "").replace("\t", "").replace(" ", "")

print lines

Upvotes: 2

Felipe
Felipe

Reputation: 161

What about the usage of the json module?

import json

tmp = json.loads(open("/path/to/file.txt", "r"))

output = open("/path/to/file2.txt", "w")
output.write(json.dumps(tmp, sort_keys=True, indent=4))

Upvotes: 0

Steven Hepting
Steven Hepting

Reputation: 12504

If you want to loop over each line, you can just:

for line in open('path/to/file.txt'):
  # Remove whitespace from both ends of line
  line = line.strip()

  # Do whatever you want with line

Upvotes: 0

Matt Feifarek
Matt Feifarek

Reputation: 520

Why not just use json.load() if the data is json?

import json
d = json.load(open('myfile.txt', 'r'))

Upvotes: 6

g.d.d.c
g.d.d.c

Reputation: 48028

Where did that structure come from? My condolences. Anyway, as a start you might try this:

cleanedData = re.sub('[\n\t]', '', f.read())

That's a brute-force removal of newline and tab characters. What it returns might be suitable for feeding into json.loads. It'll depend greatly on whether or not the contents of the file are actually valid JSON once you clear out the extra white space and line breaks.

Upvotes: 0

Related Questions