Removing characters from a .csv file that should contain only numbers


Hi,

I currently have a .csv file that should only contain numerical values however there are errors in some columns that mean there is text included. I am wondering what the best way of going about removing these characters would be.

I am looking at using str.erase(std::remove(str.begin(), str.end(), 'xxxxxxx'), str.end());. For this I will need to read my data into a string and then remove the alphabet from that string. I am currently doing this like so (I use the '?' as a delimiter because I know there are none in my file).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

string Weather_test;
char chars[] = {'A','a','B','b','C','c','D','d','E','e','F','f','G','g','H','h','I','i','J','j','K','k','L','l','M','m','N','n','O','o','P','p','Q','q','R','r','S','s','T','t','U','u','V','v','W','w','X','x','Y','y','Z','z'};

int main()
{
    ifstream Weather_test_input;
    Weather_test_input.open("/Users/MyName/Desktop/Weather_test.csv");
    
    getline(Weather_test_input, Weather_test, '?');
    
    str.erase(remove(Weather_test.begin(), Weather_test.end(), chars[!eof]), Weather_test.end();
    
    cout << Weather_test;
    
    return 0;
}


The problem with this is I don't know what to do around the chars[!eof] part. Any suggestions?

Thanks in advance!
Last edited on
I managed to do it! And for posterity, this was the solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

string Weather_test;

int main()
{
    ifstream Weather_test_input;
    Weather_test_input.open("/Users/MyName/Desktop/Weather_test.csv");
    
    getline(Weather_test_input, Weather_test, '?');
    
    Weather_test.erase(remove_if(Weather_test.begin(), Weather_test.end(), ::isalpha), Weather_test.end());
    
    cout << Weather_test;
    
    return 0;
}
Last edited on
Note that removing the character from the string you read does not remove it from the file. Also where have you declared str?

Suggestion is to read input from one file and save it in another file. In the end you can use the remove and rename functions found in cstdio to remove the old file and rename the newfile.

http://www.cplusplus.com/reference/cstdio/rename/
http://www.cplusplus.com/reference/cstdio/remove/

A rather complicated example:
http://coliru.stacked-crooked.com/a/ac761f30bb420e81

And in the end, do:
1
2
remove("C:/Users/MyName/Desktop/Weather_test.csv");
rename("C:/Users/MyName/Desktop/Weather_test_new.csv", "C:/Users/MyName/Desktop/Weather_test.csv");


Looks like OP beat me to a solution and I think I prefer his solution to mine. The one I posted is just overkill
Last edited on
Quite right, I haven't solved it. I just got a bit over excited at my ability to remove the characters from the string and thought I had.

I am now looking at how to write that string to another .csv file (should be easy)

I haven't declared str but why would I need to?

Also who is OP? And I can't see your solution. Am I missing something here?
Last edited on
Topic archived. No new replies allowed.