如何从Python中的文本文件提取表

如何从Python中的文本文件提取表,python,text,web-scraping,beautifulsoup,Python,Text,Web Scraping,Beautifulsoup,但是,它不起作用。有人能告诉我怎么做吗?我希望最后将此列D保存到csv文件中。它不起作用,因为您在一个不是html而是纯文本的文件中使用html解析器。 您需要逐行读取文件并确定您在感兴趣的表中的时间,然后解析行并查找表的结尾(实际上是下一个标题) 它可能看起来像这样(未测试): 它不起作用,因为您在一个不是html而是纯文本的文件中使用html解析器。 您需要逐行读取文件并确定您在感兴趣的表中的时间,然后解析行并查找表的结尾(实际上是下一个标题) 它可能看起来像这样(未测试): 您有纯文本,但

但是,它不起作用。有人能告诉我怎么做吗?我希望最后将此列D保存到csv文件中。

它不起作用,因为您在一个不是html而是纯文本的文件中使用html解析器。
您需要逐行读取文件并确定您在感兴趣的表中的时间,然后解析行并查找表的结尾(实际上是下一个标题)
它可能看起来像这样(未测试):


它不起作用,因为您在一个不是html而是纯文本的文件中使用html解析器。
您需要逐行读取文件并确定您在感兴趣的表中的时间,然后解析行并查找表的结尾(实际上是下一个标题)
它可能看起来像这样(未测试):


您有纯文本,但您很幸运,因为您的文本有一些格式,您可以使用:

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('\n')

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)
输出:

表格更长(43行)


您可以使用dict_表或pandas数据框,因为您有纯文本,但您很幸运,因为您的文本有一些格式,您可以使用:

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('\n')

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)
输出:

表格更长(43行)


您可以使用dict_表或pandas数据框

它给了我一个错误:
python文件“”,如果parseLines>0和line==“next heading”:^indicationError:意外未登录
@RaghavGoyal:正如我提到的-它没有经过测试,只是一个大纲,它给了我一个错误:
python文件“”,第8行如果parseLines>0且line==“下一个标题”:^IndentationError:意外的未登入
@RaghavGoyal:正如我提到的-它没有测试,只是一个大纲谢谢,这很有帮助。我真的想让它自动化。因为它以起始行和结束行作为输入。我已经做了20多年了。这将变得很麻烦。还有其他选择吗?只需找到标题所在的行,然后按名称查找列即可。如何使用python查找行?有什么我应该参考的吗。我是Python新手。这就是为什么问谢谢,这很有帮助。我真的想让它自动化。因为它以起始行和结束行作为输入。我已经做了20多年了。这将变得很麻烦。还有其他选择吗?只需找到标题所在的行,然后按名称查找列即可。如何使用python查找行?有什么我应该参考的吗。我是Python新手。这就是为什么问