读取空行csv文件



我使用的分析软件在1个csv文件中输出多组结果,并用2个空行分隔组。我想把结果分成几组,这样我就可以分别分析了。

我确信python中有一个内置函数(或它的一个库)可以做到这一点,我尝试了我在某处找到的这段代码,但它似乎不起作用。

import csv
results = open('03_12_velocity_y.csv').read().split("nn")
# Feed first csv.reader
first_csv = csv.reader(results[0], delimiter=',')
# Feed second csv.reader
second_csv = csv.reader(results[1], delimiter=',')

更新:原始代码实际上可以工作,但我的python技能相当有限,我没有正确实现它。.split(nnn)方法确实可以工作,但csv. splitReader是一个对象,要获取列表(或类似的东西)中的数据,它需要遍历所有行并将它们写入列表。然后,我使用Pandas删除标题并将科学符号值转换为浮点数。代码如下。谢谢大家的帮助。

import csv
import pandas as pd
# Open the csv file, read it and split it when it encounters 2 empty lines (nnn)
results = open('03_12_velocity_y.csv').read().split('nnn')
# Create csv.reader objects that are used to iterate over rows in a csv file
# Define the output - create an empty multi-dimensional list
output1 = [[],[]]
# Iterate through the rows in the csv file and append the data to the empty list
# Feed first csv.reader
csv_reader1 = csv.reader(results[0].splitlines(), delimiter=',')
for row in csv_reader1:
output1.append(row)
df = pd.DataFrame(output1)
# remove first 7 rows of data (the start position of the slice is always included)
df = df.iloc[7:]
# Convert all data from string to float
df = df.astype(float)

如果您的行数在组之间不一致,则需要一个小状态机来检查您何时处于组之间,并对最后一组执行一些操作。

#!/usr/bin/env python3
import csv

def write_group(group, i):
with open(f"group_{i}.csv", "w", newline="") as out_f:
csv.writer(out_f).writerows(group)

with open("input.csv", newline="") as f:
reader = csv.reader(f)
group_i = 1
group = []
last_row = []
for row in reader:
if row == [] and last_row == [] and group != []:
write_group(group, group_i)
group = []
group_i += 1
continue
if row == []:
last_row = row
continue
group.append(row)
last_row = row
# flush remaining group
if group != []:
write_group(group, group_i)

我模拟了这个示例CSV:

g1r1c1,g1r1c2,g1r1c3
g1r2c1,g1r2c2,g1r2c3
g1r3c1,g1r3c2,g1r3c3

g2r1c1,g2r1c2,g2r1c3
g2r2c1,g2r2c2,g2r2c3

g3r1c1,g3r1c2,g3r1c3
g3r2c1,g3r2c2,g3r2c3
g3r3c1,g3r3c2,g3r3c3
g3r4c1,g3r4c2,g3r4c3
g3r5c1,g3r5c2,g3r5c3
当我运行上面的程序时,我得到三个CSV文件:

group_1.csv

g1r1c1,g1r1c2,g1r1c3
g1r2c1,g1r2c2,g1r2c3
g1r3c1,g1r3c2,g1r3c3

group_2.csv

g2r1c1,g2r1c2,g2r1c3
g2r2c1,g2r2c2,g2r2c3

group_3.csv

g3r1c1,g3r1c2,g3r1c3
g3r2c1,g3r2c2,g3r2c3
g3r3c1,g3r3c2,g3r3c3
g3r4c1,g3r4c2,g3r4c3
g3r5c1,g3r5c2,g3r5c3

如果行数一致,则可以使用相当普通的Python或使用Pandas库。

香草Python

  • 定义您的组大小和组之间的分隔符(在"row "中)的大小。
  • 遍历所有行,将每一行添加到组累加器。
  • 当组累加器达到预定义的组大小时,对其进行处理,重置累加器,然后跳过中断大小的行。

这里,我将每个组写入其自己的编号文件:

import csv
group_sz = 5
break_sz = 2

def write_group(group, i):
with open(f"group_{i}.csv", "w", newline="") as f_out:
csv.writer(f_out).writerows(group)

with open("input.csv", newline="") as f_in:
reader = csv.reader(f_in)
group_i = 1
group = []
for row in reader:
group.append(row)
if len(group) == group_sz:
write_group(group, group_i)
group_i += 1
group = []
for _ in range(break_sz):
try:
next(reader)
except StopIteration:  # gracefully ignore an expected StopIteration (at the end of the file)
break

group_1.csv

g1r1c1,g1r1c2,g1r1c3
g1r2c1,g1r2c2,g1r2c3
g1r3c1,g1r3c2,g1r3c3
g1r4c1,g1r4c2,g1r4c3
g1r5c1,g1r5c2,g1r5c3
与熊猫

我是熊猫新手,边走边学,但看起来熊猫会自动从数据块中删除空白行/记录^1。

考虑到这一点,您所需要做的就是指定组的大小,并告诉Pandas以"迭代器模式"读取CSV文件,在该模式下,您可以一次请求一个块(您的组大小)记录:

import pandas as pd
group_sz = 5
with pd.read_csv("input.csv", header=None, iterator=True) as reader:
i = 1
while True:
try:
df = reader.get_chunk(group_sz)
except StopIteration:
break
df.to_csv(f"group_{i}.csv")
i += 1

熊猫添加"ID"列和默认头,当它写出CSV:

group_1.csv

,0,1,2
0,g1r1c1,g1r1c2,g1r1c3
1,g1r2c1,g1r2c2,g1r2c3
2,g1r3c1,g1r3c2,g1r3c3
3,g1r4c1,g1r4c2,g1r4c3
4,g1r5c1,g1r5c2,g1r5c3

用你的输出试试这个:

import pandas as pd
# csv file name to be read in 
in_csv = 'input.csv'
# get the number of lines of the csv file to be read
number_lines = sum(1 for row in (open(in_csv)))
# size of rows of data to write to the csv, 
# you can change the row size according to your need
rowsize = 500
# start looping through data writing it to a new file for each set
for i in range(1,number_lines,rowsize):
df = pd.read_csv(in_csv,
header=None,
nrows = rowsize,#number of rows to read at each loop
skiprows = i)#skip rows that have been read
#csv to write data to a new file with indexed name. input_1.csv etc.
out_csv = 'input' + str(i) + '.csv'
df.to_csv(out_csv,
index=False,
header=False,
mode='a', #append data to csv file
)

我用回答我的问题的最后细节更新了问题。

相关内容

  • 没有找到相关文章

最新更新