Home > Back-end >  How to read DD/MM/YY data directly from a csv file in python? (without using panda)
How to read DD/MM/YY data directly from a csv file in python? (without using panda)

Time:09-24

I need to read in data values of format "1/2/16" for example, where it represents the date 01-02-2016 in mm-dd-yyyy format. I have a csv with each column of the form "11/2/16, 0.78" for example with a value associated to the date. How should I go about loading my dates and values to plot and look for a trend.

CodePudding user response:

This approach can be used just by using built-in python methods.


If your date is in the format mm/dd/yy, x.xx, eg: "11/2/16, 0.78", you can use the below code to extract mm (month), dd (date) and yy (year)

date = "11/2/16, 0.78"
mm, dd, yy = map(int, date.split(",")[0].split("/"))
print(mm, dd, yy)

If you run the above code, it outputs:

>>> 11 2 16

But you have also mentioned another format, ie mm-dd-yyyy. If you use this format, refer below code

date = "11-2-2016"
mm, dd, yyyy = map(int, date.split("-"))
print(mm, dd, yyyy)

If you run the above code, it outputs:

>>> 11 2 2016
  • Related