[英]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.