Warning
This page is located in archive. Go to the latest version of this course pages.

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/labs/machine_learning/readcsv.txt · Last modified: 2022/05/02 15:55 by xposik