Skip to content

Commit

Permalink
Solution for delimiter reading issue utilizing dynamic checkpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
JensWendt committed Sep 6, 2023
1 parent 0ce3845 commit dd2ffab
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions omero/annotation_scripts/KeyVal_from_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import sys
import csv
import copy
from math import floor

from omero.util.populate_roi import DownloadingOriginalFileProvider

Expand Down Expand Up @@ -158,28 +159,30 @@ def keyval_from_csv(conn, script_params):
# Needs omero-py 5.9.1 or later
temp_name = temp_file.name
with open(temp_name, 'rt', encoding='utf-8-sig') as file_handle:
file_length = len(file_handle.read(-1))
try:
delimiter = csv.Sniffer().sniff(
file_handle.read(500), ",;\t").delimiter
file_handle.read(floor(file_length/4)), ",;\t").delimiter
print("Using delimiter: ", delimiter,
" after reading 500 characters")
f" after reading {floor(file_length/4)} characters")
except Exception:
file_handle.seek(0)
try:
delimiter = csv.Sniffer().sniff(
file_handle.read(1000), ",;\t").delimiter
file_handle.read(floor(file_length/2)), ",;\t").delimiter
print("Using delimiter: ", delimiter,
" after reading 1000 characters")
f" after reading {floor(file_length/2)} characters")
except Exception:
file_handle.seek(0)
try:
delimiter = csv.Sniffer().sniff(
file_handle.read(2000), ";,\t").delimiter
file_handle.read(floor(file_length*0.75)), ",;\t").delimiter
print("Using delimiter: ", delimiter,
" after reading 2000 characters")
f" after reading {floor(file_length*0.75)} characters")
except Exception:
print("Failed to sniff delimiter, using ','")
delimiter = ","

# reset to start and read whole file...
file_handle.seek(0)
data = list(csv.reader(file_handle, delimiter=delimiter))
Expand Down

0 comments on commit dd2ffab

Please sign in to comment.