简体   繁体   English

Python CSV模块:如何在同一个文件中考虑多个表?

[英]Python CSV module: How can I account for multiple tables within the same file?

I have an Excel file that I converted to CSV. 我有一个转换为CSV的Excel文件。 There are several tables each separated by an empty row. 有几个表,每个表由一个空行分隔。 After converting the Excel file to CSV, I see each empty row represented by a row of commas, with a comma for every column/field element. 将Excel文件转换为CSV后,我看到每个空行都由一行逗号表示,并且每个列/字段元素都带有一个逗号。 Can the CSV module (or some other Python module) account for multiple tables from this information? CSV模块(或其他Python模块)是否可以根据此信息说明多个表? If not, is my only option to separate the tables into different files manually in Excel before conversion? 如果不是,我唯一的选择是在转换之前在Excel中手动将表分成不同的文件吗?

I know the CSV module will turn each row into a list. 我知道CSV模块会将每一行变成一个列表。 I'd like a table to be its own list and all the rows it has as lists within. 我希望表格成为其自己的列表,并将其作为列表包含在其中的所有行。 Each table has the first row as fields. 每个表都有第一行作为字段。 The fields can be different from table to table, and the number of fields can be different as well. 表格与表格之间的字段可以不同,字段的数量也可以不同。

You can give this a try: 您可以尝试一下:

def extract_table(f):
    table = []
    for line in f:

        if not len(line):
            # Table delimeter reached
            break

        fields = line.split(',')
        table.append(fields)
    return table

def main():

    with open("myfile.csv") as f:
        while True:
            table = extract_table(f)

            if not len(table):
                # No table found, reached end of file
                break

            # Do something with table
            # ...

Sure, it's easy to read the data in that way. 当然,以这种方式读取数据很容易。 You have to decide what constitutes the separator row (is it sufficient to check for the first column being empty, or do you have to check that all columns are empty?) Assuming just the first row (and being extra verbose for clarity): 您必须决定什么构成分隔符行(检查第一列是否为空是否足够,还是必须检查所有列是否为空?)假设仅第一行(为清楚起见,要特别冗长):

 rdr = csv.reader(open(filename))

 tables = []
 this_table = []
 tables.append(this_table)
 for row in rdr:
      if row[0] is None:
         this_table = []
         tables.append(this_table)
      this_table.append(row)

The result is a list called tables. 结果是一个称为表的列表。 Each entry is a list containing the data for one table. 每个条目都是一个列表,其中包含一个表的数据。 Each entry in a table is a list containing the column values for one row. 表中的每个条目都是一个列表,其中包含一行的列值。

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

相关问题 如何从 python 模块代码制作 csv 文件? - How can I make a csv file from a python module code? 如何读取在同一单元格中有多行的 csv 文件? - how to read a csv file that has multiple lines within the same cell? 如何使用 python 将 csv 文件可视化为表格? - How can I visualise csv files as tables using python? 如何导入多个 JSON 文件并使用 Python 将它们转储到同一个 CSV 上? - How can I import multiple JSON files and dump them on the same CSV using Python? 如何使用python遍历多个csv文件,如果值相同,则更新另一个值? - How can I use python to iterate through multiple csv files and if a value is the same, update another value? 如何在 python 中使用 CSV 文件? - How can I use a CSV file in python? 如何合并具有相同列名但每个文件具有不同日期的多个.CSV 文件? - How can I merger multiple .CSV files with same column names but each file has different dates? 如何从多个 url 中抓取数据并将这些数据保存在同一个 csv 文件中? - How can I scrape data from multiple urls and save these data in the same csv file? 如何导入 Python 模块并让它在我的主文件中工作? - How can I import a Python module and have it work within my main file? 如何比较python中csv文件的列中的值? - How do I compare values within a column in a csv file in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM