在 Python 中拆分 CSV 文件,用分号分隔记录

Split CSV file in Python with semicolon separating the records

我有一个包含 288 条记录的 CSV 文件,格式如下(仅提取前 3 条记录)

20210402,23:55,37684,4.758,0,0,0.000,16238,510,NaN,242.0,-500.000,0.000,500.000,-500.000,10.000,NaN;20210402,23:50,37684 ,4.758,0,0,0.000,16195,540,NaN,243.0,-530.000,0.000,530.000,-530.000,10.000,NaN;20210402,23:45,37684,4.758,0,0,0.000,16150,540 ,NaN,243.0,-550.000,0.000,550.000,-550.000,0.000,NaN;

如果我用记事本打开这个文件,它会保存为连续的字符串。如果我在 excel 中打开它,那么每个值都在一列中。第一条记录的最后一列与借调记录的第一列共享,依此类推,如下所示

我正在尝试吐出字符串,因此有 288 条记录使用

with open('dailyData.csv','r') as file:
    array = file.readlines()
    array = [row.split(';') for row in array]

“;”转换为“,”但不拆分字符串

[['20210402,23:55,37684,4.758,0,0,0.000,16238,510,NaN,242.0,-500.000,0.000,500.000,-500.000,10.000,NaN', '20210402, 23:50,37684...

有什么想法吗?

更新

我试过Pandas

array = pandas.read_csv('dailyData.csv', delimiter=';', header=None)
print(array.columns)

这个returns

Int64Index([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ... 277, 278, 279, 280, 281, 282, 283, 284, 285, 286], dtype='int64', 长度=287)

这意味着我可以寻址任何记录,但不能寻址记录中的元素。

只是希望能够作为数组[r][c] 寻址,其中 r 是 0 到 287,c 是 0 到 16。

谢谢

嗨,卡米尔,我试过了

 with open('dailyData.csv') as file:
    for x in file:
        columns = x.split(';')
        for y in columns:
            lines = y.split(',')
            print(lines)

这会打印行,但不会创建我可以寻址的数组。

with open('dailyData.csv','r') as file:
    array = file.readlines()
    array = [x.split(',') for x in row.split(';') for row in array]

这个怎么样:

with open("data.csv") as f:
    array = [l.split(",") for l in f.readline().split(";") if l]

print(len(array))
print(array[1][0])

输出:其中 3 是数组中列表的数量,每个列表有 16 个值。

3
20210402

以上允许:

Just looking to be able to address as array[r][c] where r is 0 to 287 and c is 0 to 16.

我假设你的数据是一长串连续的字符串,如你的问题所示。

如果您愿意,可以轻松地将其转储到 pandas DataFrame,然后转储到适当的 .csv 文件:

import pandas as pd

with open("data.csv") as f:
    array = [l.split(",") for l in f.readline().split(";") if l]

pd.DataFrame(array).to_csv("your_array.csv", header=False, index=False)