Videos
For context: I'd like to compare election results over multiple elections from the past few years in my district.
The data I got access to is quite a broad arrangement of text files with pages of data separated by varying amounts of semicolons. I tried to simply convert to .csv, but that way excel just sorts them into like a thousand columns, but doesn't add a single row.
I tried the webversion, but next to the fact that its very glitchy, the whole split into columns isn't particularly useful if I don't want to spend hours dragging data vertically.
What I like to do is arrange the data quite simply by minor district, amount of voters, amount of votes for party A and so on, and ultimately compare it with other elections in a single sheet.
Do you have any idea how I could import the data in a way that will save me all that? In theory, it seems pretty simple, but right now it seems impossible to me, despite having all the data delivered on a silver plate
Try OpenCSV - it will make your life easier.
First, add this package to your gradle dependencies as follows
implementation 'com.opencsv:opencsv:4.6'
Then you can either do
import com.opencsv.CSVReader;
import java.io.IOException;
import java.io.FileReader;
...
try {
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
System.out.println(nextLine[0] + nextLine[1] + "etc...");
}
} catch (IOException e) {
}
or
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
List myEntries = reader.readAll();
Edit after comment
try {
File csvfile = new File(Environment.getExternalStorageDirectory() + "/csvfile.csv");
CSVReader reader = new CSVReader(new FileReader(csvfile.getAbsolutePath()));
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
System.out.println(nextLine[0] + nextLine[1] + "etc...");
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "The specified file was not found", Toast.LENGTH_SHORT).show();
}
If you want to package the .csv file with the application and have it install on the internal storage when the app installs, create an assets folder in your project src/main folder (e.g., c:\myapp\app\src\main\assets\), and put the .csv file in there, then reference it like this in your activity:
String csvfileString = this.getApplicationInfo().dataDir + File.separatorChar + "csvfile.csv"
File csvfile = new File(csvfileString);
The following snippet reads a CSV file from the raw resources folder (which will be packed into your .apk file upon compilation).
Android by default does not create the raw folder. Create a raw folder under res/raw in your project and copy your CSV File into it. Keep the name of the CSV file lower case and convert it into text format when asked. My CSV file name is welldata.csv.
In the snippet, WellData is the model class (with constructor, getter and setter) and wellDataList is the ArrayList to store the data.
private void readData() {
InputStream is = getResources().openRawResource(R.raw.welldata);
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, Charset.forName("UTF-8")));
String line = "";
try {
while ((line = reader.readLine()) != null) {
// Split the line into different tokens (using the comma as a separator).
String[] tokens = line.split(",");
// Read the data and store it in the WellData POJO.
WellData wellData = new WellData();
wellData.setOwner(tokens[0]);
wellData.setApi(tokens[1]);
wellData.setLongitude(tokens[2]);
wellData.setLatitude(tokens[3]);
wellData.setProperty(tokens[4]);
wellData.setWellName(tokens[5]);
wellDataList.add(wellData);
Log.d("MainActivity" ,"Just Created " + wellData);
}
} catch (IOException e1) {
Log.e("MainActivity", "Error" + line, e1);
e1.printStackTrace();
}
}
There are file explorer style apps that will open it. Or you could download Csv Viewer app from Play Store.
Blurb from it.
CSV File Viewer For Android | CSV Reader - view small and large-sized CSV files.
CSV Reader - Simple, fast, and powerful tool for reading csv files.
I am not associated with it.
Any office suite should be able to open it. I recommend using CollaboraOffice (https://play.google.com/store/apps/details?id=com.collabora.libreoffice&hl=en_US), I successfully opened .csv files with it in table format and can also edit it. The app has no ads and it is open source.
(I am not promoting/endorsing the app)