简体   繁体   中英

Exporting data from python to text file using petl package in python

I am trying to extract raw data from a text file and after processing the raw data, I want to export it to another text file. Below is the python code I have written for this process. I am using the "petl" package in python 3 for this purpose. 'locations.txt' is the raw data file.

import glob, os
from petl import *


class ETL():

    def __init__(self, input):
        self.list = input


    def parse_P(self):
        personids = None
        for term in self.list:
            if term.startswith('P'):
                personids = term[1:]
        personid = personids.split(',')

        return personid

    def return_location(self):
        location = None
        for term in self.list:
            if term.startswith('L'):
                location = term[1:]
        return location

    def return_location_id(self, location):
        location = self.return_location()
        locationid = None


    def return_country_id(self):
        countryid = None
        for term in self.list:
            if term.startswith('C'):
                countryid = term[1:]

        return countryid

    def return_region_id(self):
        regionid = None
        for term in self.list:
            if term.startswith('R'):
                regionid = term[1:]

        return regionid

    def return_city_id(self):
        cityid = None
        for term in self.list:
            if term.startswith('I'):
                cityid = term[1:]
        return cityid

print (os.getcwd())
os.chdir("D:\ETL-IntroductionProject")
print (os.getcwd())
final_location = [['L','P', 'C', 'R', 'I']]

new_location = fromtext('locations.txt', encoding= 'Latin-1')
stored_list = [] 
for identifier in new_location:
    if identifier[0].startswith('L'):
        identifier = identifier[0]
        info_list = identifier.split('_')
        stored_list.append(info_list)

for lst in stored_list:
    tabling = ETL(lst)
    location = tabling.return_location()
    country = tabling.return_country_id()
    city = tabling.return_city_id()
    region = tabling.return_region_id()
    person_list = tabling.parse_P()
    for person in person_list:
        table_new = [location, person, country, region, city]
        final_location.append(table_new)

totext(final_location, 'l1.txt')

However when I use "totext" function of petl, it throws me an "Assertion Error".

AssertionError: template is required I am unable to understand what the fault is. Can some one please explain the problem I am facing and what I should be doing ?

The template parameter to the toext function is not optional there is no default format for how the rows are written in this case, you must provide a template. Check the doc for toext here for an example: https://petl.readthedocs.io/en/latest/io.html#text-files

The template describes the format of each row that it writes out using the field headers to describe things, you can optionally pass in a prologue to write the header too. A basic template in your case would be:

table_new_template = "{L} {P} {C} {R} {I}" totext(final_location, 'l1.txt', template=table_new_template)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM