Iterate the contents of a text file in python

I have a text file called "triple_response.txt" that contains some text like:

(1,(db_name,string),DSP)
(1,(rel, id),2)
(2,(rel_name, string),DataSource)
(2,(tuple, id),201)
(2,(tuple, id),202)
(2,(tuple, id),203)
(201,(src_id,varchar),Pos201510070)
(201,(src_name,varchar),Postgres)
(201,(password,varchar),root)
(201,(host,varchar),localhost)
(201,(created_date,date),2015-10-07)
(201,(user_name,varchar),postgres)
(201,(src_type,varchar),Structured)
(201,(db_name,varchar),postgres)
(201,(port,numeric),None)
(202,(src_id,varchar),pos201510060)
(202,(src_name,varchar),Postgres)
(202,(password,varchar),root)
(202,(host,varchar),localhost)
(202,(created_date,date),2015-10-06)
(202,(user_name,varchar),postgres)
(202,(src_type,varchar),Structured)
(202,(db_name,varchar),DSP)
(202,(port,numeric),5432)
(203,(src_id,varchar),pos201510060)
(203,(src_name,varchar),Postgres)
(203,(password,varchar),root)
(203,(host,varchar),localhost)
(203,(created_date,date),2015-10-06)
(203,(user_name,varchar),postgres)
(203,(src_type,varchar),Structured)
(203,(db_name,varchar),maindb)
(203,(port,numeric),5432)

I am trying to convert this content to JSON using a python script:

import re
import collections
import json, jsonpickle


def convertToJSON(File):
    word_list=[]
    row_list = []
    try:
        with open(File,'r') as f:
            for word in f:
                word_list.append(word)


        with open(File,'r+') as f:
            for row in f:
                print row
                row_list.append(row.split())

        column_list = zip(*row_list)
    except IOError:
        print "Error in opening file.."
    triple =""
    for t in word_list:
        triple+=t

    tripleList = re.findall(r"\([^\(^\)]*\)",triple)
    idList = re.split(r"\([^\(^\)]*\)",triple)

    i =0
    jsonDummy = []
    jsonData = {}
    for trip in tripleList:
        nameAndType = re.split(r",|:",trip)

        if(i==0):
                key = re.compile("[^\w']|_").sub("",idList[i])
        else:
            try:
                key = re.compile("[^\w']|_").sub("",idList[i].split("(")[1])
            except IndexError:
                pass
        i = i+1
        if(idList[i].find('(')!=-1):
            try:
                content = re.compile("[^\w']|_").sub("",idList[i].split(")")[0])

            except IndexError:
                pass
        else:
            content = re.compile("[^\w']|_").sub("",idList[i])
        try:
            trip = trip[1:-1]
            tripKey = trip[1]

        except IndexError:
            tripKey = ''
        name = re.compile("[^\w']").sub("",nameAndType[0])
        try:
            typeName = re.compile("[^\w']|_").sub("",nameAndType[1])
        except IndexError:
            typeName = 'String'

        tripDict = dict()
        value = dict()

        value[name] = content
        tripDict[key]=value

        jsonDummy.append(tripDict)

    for j in jsonDummy:
        for k,v in j.iteritems():
            jsonData.setdefault(k, []).append(v)

    data = dict()
    data['data'] = jsonData
    obj = {}
    obj=jsonpickle.encode(data, unpicklable=False)

    return obj

    pass

I call this convertToJSON () function in the same file as:

print convertToJSON ("triple_response.txt")

I get the output as I expect:

{"data": {"1": [{"db_name": "DSP"}, {"rel": "2"}], "201": [{"src_id": "Pos201510070"}, {"src_name": "Postgres"}, {"password": "root"}, {"host": "localhost"}, {"created_date": "20151007"}, {"user_name": "postgres"}, {"src_type": "Structured"}, {"db_name": "postgres"}, {"port": "None"}], "203": [{"src_id": "pos201510060"}, {"src_name": "Postgres"}, {"password": "root"}, {"host": "localhost"}, {"created_date": "20151006"}, {"user_name": "postgres"}, {"src_type": "Structured"}, {"db_name": "maindb"}, {"port": "5432"}], "2": [{"rel_name": "DataSource"}, {"tuple": "201"}, {"tuple": "202"}, {"tuple": "203"}], "202": [{"src_id": "pos201510060"}, {"src_name": "Postgres"}, {"password": "root"}, {"host": "localhost"}, {"created_date": "20151006"}, {"user_name": "postgres"}, {"src_type": "Structured"}, {"db_name": "DSP"}, {"port": "5432"}]}}

Now the problem I am facing that I am calling from outside the class:

def extractConvertData(self):
       triple_response = SPO(source, db_name, table_name, response)
       try:
           _triple_file = open('triple_response.txt','w+')
           _triple_file.write(triple_response)
           print "written data in file.."
           with open('triple_response.txt','r+') as f:
                for word in f:
                    print word
           jsonData = convertToJSON(str('triple_response.txt')) 
       except IOError:
           print "Not able to open a file"
       print "Converted into JSON"
       print jsonData
       pass

The same convertToJSON () code does not work ...

It does not give any output and does not give any error, it cannot read the contents from the file 'triple_response.txt' in the line.

with open('triple_response.txt','r+') as f:
    for word in f:
        print word

Anyone can tell me a solution to this problem.

+4
source share
1 answer

_triple_file ( , Python, ).

, , ( ? Unix? Windows?). , _triple_file . . : (_triple_file.write(triple_response)). , , os.stat(), .

, ... except, , . ... _triple_file, . (, tempfile , , ).

- :

   triple_response = SPO(source, db_name, table_name, response)
   try:
       _triple_file = open('triple_response.txt','w+')
       _triple_file.write(triple_response)
       _triple_file.close()
   except IOError:
       print "Not able to write intermediate JSON file"
       raise 

   assert [suitable expression involving os.stat('triple_response.txt') to test size > 0 ], "Error: intermediate JSON file was empty"

   try:
       with open('triple_response.txt','r+') as f:
            for word in f:
                print word
       jsonData = convertToJSON(str('triple_response.txt')) 
   except IOError:
       print "Not able to read back intermediate JSON file"
       #raise  # if you want to reraise the exception

   ...
+2

Source: https://habr.com/ru/post/1611383/


All Articles