Warning
This page is located in archive.

Reading .dsv files into a dictionary

To work with the data from file truth.dsv, it will be convenient to read them into a dictionary which would tell you for each file what numeral or character is depicted on the image.

Reading the .dsv file manually

Reading the data into a dictionary is not hard, you can try to implement it yourself:

  • open the file for reading
  • for each line in the file:
    • remove the white space in the beginning and at the end of the line using str.strip()
    • call str.split(':') method that will split the line into two parts using the colon as a separator
    • create key-value pair in the dictionary using the first part of the line as key and the second part as value

Take advantage of ''csv'' module

Python contains standard module csv for easier work with .csv, .dsv or similar file formats. You can use it to read the truth.dsv file in a similar way as described here. You will only have to correctly specify the separator, i.e., set the parameter delimiter=“:” when calling csv.reader().

Nevertheless, in this particular case the use of csv module will not save you much work (in comparison to manual processing of the .csv file as suggested above).

courses/be5b33kui/semtasks/05_ml1/readcsv.txt · Last modified: 2024/02/18 20:09 by xposik