简体   繁体   English

如何从 CSV 文件中删除所有字母?

[英]How do I remove all the letters from a CSV file?

How would I go about removing letters and numbers except 1s and 0s from my CSV file.我将如何从 CSV 文件中删除除 1 和 0 之外的字母和数字。 I have tried using Pandas but I do not think that I used it correctly to accomplish the task.我曾尝试使用 Pandas,但我认为我没有正确使用它来完成任务。

Assuming you have already loaded this file into a matrix (stored in variable csv here):假设您已经将此文件加载到矩阵中(此处存储在变量csv中):

for x in range(len(csv)):
    for y in range(len(csv[x])):
        if csv[x][y] not in ["1", "0"]:
            csv[x][y] = ""

This code will change all characters, that are not 1s or 0s to empty strings.此代码会将所有非 1 或 0 的字符更改为空字符串。 If necessary, you can easily modify this code to change them to any other value, or completely remove them from the matrix (however, in this case, while loops are necessary).如有必要,您可以轻松修改此代码以将它们更改为任何其他值,或者将它们从矩阵中完全删除(但是,在这种情况下,需要使用while循环)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM