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.
Asked
Active
Viewed 146 times
0
-
1Have a look at this: https://stackoverflow.com/questions/55404141/how-to-read-a-csv-file-without-using-external-libraries-such-as-numpy-pandas. – Amal Sailendran Sep 24 '21 at 05:36
-
I assume each line is stored in variable `line`, the you can get the date in this way:`line.split(',')[0].split('/')`. It will give you a list containing `"11", "2", "16"`. If you want them to be `int`, then, this will work: `map(int,line.split(',')[0].split('/') )` – Amir reza Riahi Sep 24 '21 at 05:42
-
1Can you show something you tried first? For reading a csv in Python, actually you do not need *any* libraries (but likely you gonna use them to get more robust code). For parsing date/time, you have the datetime library with strptime. – FObersteiner Sep 24 '21 at 05:43
1 Answers
1
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

abhira0
- 280
- 1
- 11