从大CSV中删除逗号(1GB)

use*_*235 7 python csv matlab comma

我有一个大的CSV文件(1GB),我想从中删除逗号.数据都是正整数.我尝试过的方法包括带空格作为分隔符的dlmwrite,但输出则以十进制格式输出.我也尝试使用fprintf命令,但后来我失去了矩阵的形状(即所有数据都出现在一行或一列中).

从而,

是否有一种从CSV(input.txt)读入的简单方法:

1, 2, 3, 4, 5
2, 3, 4, 5, 6
Run Code Online (Sandbox Code Playgroud)

然后以下列形式输出到文本文件(output.txt):

1 2 3 4 5
2 3 4 5 6
Run Code Online (Sandbox Code Playgroud)

Tim*_*ker 10

在Python中,如果格式非常简单(并且每个逗号后面都有空格):

with open("infile.csv") as infile, open("outfile.csv", "w") as outfile:
    for line in infile:
        outfile.write(line.replace(",", ""))
Run Code Online (Sandbox Code Playgroud)

如果你不能确定空格:

import re
with open("infile.csv") as infile, open("outfile.csv", "w") as outfile:
    for line in infile:
        outfile.write(re.sub(r"\s*,\s*", " ", line))
Run Code Online (Sandbox Code Playgroud)


mel*_*aco 0

您可以使用 fgetl 从文件描述符中逐行读取,如下所示:

fid=fopen('file.csv');
if (fid==-1)
    return
end
sl=fgetl(fid);        
while (~feof(fid))
    sl=fgetl(fid);  
    icol=find(sl==',');
end  

fclose(fid);
Run Code Online (Sandbox Code Playgroud)

在 sl 中,您可以用空格替换 , 并再次写入磁盘