简体   繁体   English

使用python中的petl包将数据从python导出到文本文件

[英]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. 以下是我为此过程编写的python代码。 I am using the "petl" package in python 3 for this purpose. 我为此使用python 3中的“ petl”包。 'locations.txt' is the raw data file. “ locations.txt”是原始数据文件。

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". 但是,当我使用petl的“ totext”功能时,会抛出“断言错误”。

AssertionError: template is required I am unable to understand what the fault is. AssertionError:需要模板,我无法理解故障所在。 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. toext函数的template参数不是可选的,在这种情况下,没有默认的行写格式,必须提供一个模板。 Check the doc for toext here for an example: https://petl.readthedocs.io/en/latest/io.html#text-files 在此处检查toext的文档以获取示例: 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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