Good afternoon people! I have been facing a problem creating an Android app, and I would like to know if you can help me. I developed a program in python (tkinter), where the user selects the directory where the files are (there are .pssesion and .csv files, the user needs the csv). Once the directory is selected, all csv files are selected (in this case, the first 6 lines are deleted, and only the first 16 lines of column 5 are selected), finally, there is a transposition of the data to the horizontal, and the file names are added to the index, as printed(the link to test https://drive.google.com/drive/folders/1_h3QdCMiWQ9uVQh_DiUB2RFI_VTKZhaR?usp=drive_link):
Below is the function activated by the button in python
def dir_select():
selected_dir = filedialog.askdirectory(title="Escolha a pasta")
if not selected_dir:
return
os.chdir(selected_dir)
csv_files = glob.glob('*csv')
list_map = map(lambda filename:
pd.read_csv(filename, header=None, encoding="UTF-16 LE",
engine='python', sep=",", skiprows=[0, 1, 2, 3, 4, 5],
nrows = 16, usecols=[4]), csv_files)
list_file = list(list_map)
df = pd.concat(list_file, axis=1, ignore_index=True).T
#get the file name and add as index
index = []
for a in csv_files:
if a.endswith('.csv'):
index.append(a[:-4])
df.index = index
columns_names = ['I16', 'I15', 'I14', 'I13', 'I12', 'I11', 'I10', 'I9',
'I8', 'I7', 'I6', 'I5', 'I4', 'I3', 'I2', 'I1']
df.columns = columns_names
print(df)
return df
index 0 1 2 ... 15
2022-031853-07_1 123.543000 166.202000 223.808000 ... 8383.000000
2022-031853-07_2 122.386558 164.766866 222.529414 ... 8349.414025
2022-031853-07_3 123.130502 166.088386 224.333815 ... 8349.645300
KCL_1 1565.487225 1838.774695 2264.166823 ... 68266.129876
KCL_2 1522.474882 1800.673612 2209.711340 ... 69398.544797
I've tried several things using chatgpt and Bard, but I'm stuck here. I can access the directory, but it returns null or [ ]. how to rewrite this function for kotlin, correctly?
val button = findViewById<Button>(R.id.select_button)
button.setOnClickListener {
// Launch the document tree picker intent
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
directoryPickerResultLauncher.launch(intent)
}
}
private val directoryPickerResultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
// Get the selected directory URI
val uri = result.data?.data
if (uri != null) {
selectedDirectoryUri = uri.toString()
// List files in the selected directory and filter by .csv extension
val selectedDirectory = File(uri.path!!)
val dataFiles = selectedDirectory.listFiles { file ->
file.isFile && file.name.endsWith(".csv")
}
println("Entrou aqui - 1 ")
println(selectedDirectory)
println(dataFiles)
// Process the selected files
handlingFiles(dataFiles)
}
}
}
private fun handlingFiles(dataFiles: Array<File>?): List<Map<String, String>> {
val columnsNames = listOf(
"I16", "I15", "I14", "I13", "I12", "I11", "I10", "I9",
"I8", "I7", "I6", "I5", "I4", "I3", "I2", "I1"
)
val resultData = mutableListOf<Map<String, String>>()
if (dataFiles != null) {
for (file in dataFiles) {
val csvData = mutableListOf<Map<String, String>>()
csvReader().open(file) {
readAllAsSequence().forEach { row ->
val rowData = mutableMapOf<String, String>()
for ((index, value) in row.withIndex()) {
if (index < columnsNames.size) {
rowData[columnsNames[index]] = value
}
}
csvData.add(rowData)
println(rowData)
}
}
resultData.addAll(csvData)
}
}
// Log the resultData
for ((index, row) in resultData.withIndex()) {
Log.d("CSV Data", "Row $index: $row")
}
return resultData
}
companion object {
private const val REQUEST_DIRECTORY = 123
}
}