Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

how to get the table contents from a file in python

import requests
import pandas as pd

txt_data = requests.get('https://downloads.usda.library.cornell.edu/usda-esmis/files/c821gj76b/n870zs10r/h989r4519/AgriPric-03-30-2017.txt').text
splited_data = txt_data.split('
')

table_title = 'Prices Received for Field Crops and Fruits - United States: February 2017 with Comparisons'
END_TABLE_LINE = '-------------------------------------------'


def find_no_line_start_table(table_title,splited_data):
    found_no_lines = []
    for index, line in enumerate(splited_data):
        if table_title in line:
            found_no_lines.append(index)

    return found_no_lines

_, table_start = find_no_line_start_table(table_title,splited_data)


def get_start_data_table(table_start, splited_data):
    for index, row in enumerate(splited_data[table_start:]):
        if '(D)' in row:
            return table_start + index

def get_end_table(start_table_data, splited_data ):
    for index, row in enumerate(splited_data[start_table_data:]):
            if END_TABLE_LINE in row:
                return start_table_data + index

def row(l):
    l = l.split()
    number_columns = 5
    if len(l) >= number_columns: 
        data_row = [''] * number_columns
        first_column_done = False

        index = 0
        for w in l:
            if not first_column_done:
                data_row[0] = ' '.join([data_row[0], w])
                if ':' in w:
                        first_column_done = True
            else:
                index += 1
                data_row[index] = w

        return data_row

start_line = get_start_data_table(table_start, splited_data)
end_line = get_end_table(start_line, splited_data)

table = splited_data[start_line : end_line]

def take_table(txt_data):
    comodity = []
    price_2011 = []
    feb_2016 = []
    jan_2017 = []
    feb_2017 = []

    for r in table:
        data_row = row(r)
        if data_row:
            col_1, col_2, col_3, col_4, col_5 = data_row
            comodity.append(col_1)
            price_2011.append(col_2)
            feb_2016.append(col_3)
            jan_2017.append(col_4)
            feb_2017.append(col_5)

    table_data = {'comodity': comodity, 'price_2011': price_2011,
                  'feb_2016': feb_2016, 'jan_2017': jan_2017, 'feb_2017': feb_2017}
    return table_data

dict_table = take_table(txt_data)
pd.DataFrame(dict_table)
Comment

PREVIOUS NEXT
Code Example
Typescript :: display entry count for specific column using value_counts spyder. 
Typescript :: ts Strategy pattern 
Typescript :: ts date get minutes 
Typescript :: type assertions in typescript 
Typescript :: gettime is not a function typescript 
Typescript :: graphql server cannot be reached 
Typescript :: how to compare two date in typescript 
Typescript :: dart clone list 
Typescript :: Interface with custom property name type 
Typescript :: swift check if file exists in bundle swift 
Typescript :: react components for login 
Typescript :: conditional type typescript 
Typescript :: ERROR Error: mat-form-field must contain a MatFormFieldControl. 
Typescript :: ts compile command 
Typescript :: react typescript append to array 
Typescript :: classes in ts 
Typescript :: typescript interview questions 
Typescript :: typescript to c# converter 
Typescript :: rascal npm 
Typescript :: Count by One Variable 
Typescript :: count file lines in typescript 
Typescript :: interface extending mongoose document object does not contain _doc object typescript 
Typescript :: A tuple type element list cannot be empty. 
Typescript :: c++ program to separate unique elements of array 
Typescript :: sum the digits in c 
Typescript :: array of linked lists in cpp 
Typescript :: Do feral cats have rabies? 
Typescript :: delete the last string from file in typescript 
Typescript :: How to load plugin scripts in roblox studio command 
Typescript :: which document is created by system analyst after the requirements are collected from various stakeholders 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =