gpt4 book ai didi

python - 处理大型 CSV 文件的文件夹

转载 作者:行者123 更新时间:2023-12-01 03:18:08 25 4
gpt4 key购买 nike

我有一个包含大型 CSV 文件的大文件夹(文件夹中大约有 25,000 个文件,并且还会进一步增加),也就是说,几乎所有文件的行数都超过了 Excel 的行限制(限制是 100 万左右,我猜测)。所有这些 CSV 文件每行都有 5 个由逗号分隔的元素,所有文件中的行数(信息)各不相同。

One CSV File:
a1,b1,c1,d1,e1
a2,b2,c2,d2,e2
.
.
.
a3152685,b3152685,c3152685,d3152685,e3152685

My Reference File:
x1,y1
x2,y2
x3,y3
.
.
.
x397,y397

本质上,我只需要根据我的引用文件 Access 每个 CSV 文件中的部分行(大约 400 行)。只要我可以在任何 CSV 文件中将 x-y 对与 a-b 对进行匹配,我就会将带有 CSV 文件标题的 a、b、c、d、e 行保存到其他位置,最好是 Excel 文件,但我愿意接受想法.

我可以使用 Matlab、Python 2.7、MS Access(如果我不必为每个文件都执行此操作,将 CSV 文件转换为数据库文件似乎是个好主意 - 是否有批处理版本可以执行此操作)或 MS Excel。我从来没有做过任何 VBA 的东西,但如果你有一些 VBA 解决方案来解决这个问题,我也愿意听。

如果您需要更多说明,请告诉我,以防我不够清楚。

最佳答案

你可以找到office的限制products here

Matlab 非常适合处理这种大文件和大量文件。 2014 版对引入 csv 数据存储进行了很多改进,现在也可以很好地处理 excel 文件。

看一下本教程:

http://blogs.mathworks.com/loren/2014/12/03/reading-big-data-into-matlab/

我有 3 个 csv 文件(文件[1-3].csv),其中包含以下内容:

a1,b1,c1,d1,e1
a2,b2,c2,d2,e2
a3,b3,c3,d3,e3
a4,b4,c4,d4,e4
a5,b5,c5,d5,e5
a6,b6,c6,d6,e6
a7,b7,c7,d7,e7
a8,b8,c8,d8,e8
a9,b9,c9,d9,e9
a10,b10,c10,d10,e10

和一个用于列名称的文件 varnames:

A B C D E

让我们阅读这些文件:

>> datafile = 'csv-files/file1.csv';
>> headerfile = 'csv-files/varnames.txt'

>> fileID = fopen(headerfile);
>> varnames = textscan(fileID,'%s');
>> varnames = varnames{:};

ds = datastore(datafile,'ReadVariableNames',false);

>> ds.VariableNames = varnames


ds =

TabularTextDatastore with properties:

Files: {
'/home/anquegi/learn/matlab/stackoverflow/csv-files/file1.csv'
}
FileEncoding: 'UTF-8'
ReadVariableNames: false
VariableNames: {'A', 'B', 'C' ... and 2 more}

Text Format Properties:
NumHeaderLines: 0
Delimiter: ','
RowDelimiter: '\r\n'
TreatAsMissing: ''
MissingValue: NaN

Advanced Text Format Properties:
TextscanFormats: {'%q', '%q', '%q' ... and 2 more}
ExponentCharacters: 'eEdD'
CommentStyle: ''
Whitespace: ' \b\t'
MultipleDelimitersAsOne: false

Properties that control the table returned by preview, read, readall:
SelectedVariableNames: {'A', 'B', 'C' ... and 2 more}
SelectedFormats: {'%q', '%q', '%q' ... and 2 more}
ReadSize: 20000 rows


>> preview(ds)

ans =

A B C D E
____ ____ ____ ____ ____

'a1' 'b1' 'c1' 'd1' 'e1'
'a2' 'b2' 'c2' 'd2' 'e2'
'a3' 'b3' 'c3' 'd3' 'e3'
'a4' 'b4' 'c4' 'd4' 'e4'
'a5' 'b5' 'c5' 'd5' 'e5'
'a6' 'b6' 'c6' 'd6' 'e6'
'a7' 'b7' 'c7' 'd7' 'e7'
'a8' 'b8' 'c8' 'd8' 'e8'

如果我们看参数ReadSize我们取的是ReadSize:20000行,所以matlab每次读取20000行就可以处理了。由于数据只有 10 行,我将其更改为 3 行:

>> ds.ReadSize=3

ds =

TabularTextDatastore with properties:

Files: {
'/home/anquegi/learn/matlab/stackoverflow/csv-files/file1.csv'
}
FileEncoding: 'UTF-8'
ReadVariableNames: false
VariableNames: {'A', 'B', 'C' ... and 2 more}

Text Format Properties:
NumHeaderLines: 0
Delimiter: ','
RowDelimiter: '\r\n'
TreatAsMissing: ''
MissingValue: NaN

Advanced Text Format Properties:
TextscanFormats: {'%q', '%q', '%q' ... and 2 more}
ExponentCharacters: 'eEdD'
CommentStyle: ''
Whitespace: ' \b\t'
MultipleDelimitersAsOne: false

Properties that control the table returned by preview, read, readall:
SelectedVariableNames: {'A', 'B', 'C' ... and 2 more}
SelectedFormats: {'%q', '%q', '%q' ... and 2 more}
ReadSize: 3 rows

>> reset(ds)
while hasdata(ds)
T = read(ds);
T.A
end

ans =

'a1'
'a2'
'a3'


ans =

'a4'
'a5'
'a6'


ans =

'a7'
'a8'
'a9'


ans =

'a10'

那么T变量就是一个表,你可以把它写在你想要的地方:注意,每次read(ds)它都会移动readsie分配的行数,这个参数可以是行,也可以是文件

>> reset(ds)
>> T = read(ds);
>> T

T =

A B C D E
____ ____ ____ ____ ____

'a1' 'b1' 'c1' 'd1' 'e1'
'a2' 'b2' 'c2' 'd2' 'e2'
'a3' 'b3' 'c3' 'd3' 'e3'

>> writetable(T,'mySpreadsheet','FileType','spreadsheet')
>> reset(ds)

关于python - 处理大型 CSV 文件的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42266914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com