Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
notation
#8
(Apr-14-2023, 10:45 AM)DeaD_EyE Wrote: I often use generators to process something line by line or element by element.
To convert a date, you should read the docs: https://docs.python.org/3/library/dateti...e.strptime

An example:
import io
from datetime import date as Date
from datetime import datetime as DateTime

# 10 lines of fake data
fake_file = io.StringIO("19260701,0.09,-0.22,-0.30,0.009\n" * 10)


def transformer(file):
    """
    Generator to exctract line by line data from a file.
    The seperator is a `,` and the first colum is a date formatted as: `YYYYMMDD`
    The remaing columns are converted to float.
    """
    # iterating over a file-object yields lines
    # and the line ending is kept
    for line in fake_file:
        # date is the first element of row
        # and *rest consumes all remaining elements
        # (rest is a list)
        # rstrip removes tailing whitespaces
        date, *rest = line.rstrip().split(",")
        # converting all elements from rest to float
        values = list(map(float, rest))

        # extracting the date
        # a regex could be used
        # but in this example I use slicing
        # year, month, day = map(int, (date[0:4], date[4:6], date[6:8]))
        # date = Date(year, month, day)

        # Another method to parse date on a single line
        date = DateTime.strptime(date, "%Y%m%d").date()
        # using iterable unpacking to get a flat list with
        # date, value1, value2, value3, ...
        yield [date, *values]


for row in transformer(fake_file):
    print(row)
Output:
[datetime.date(1926, 7, 1), 0.09, -0.22, -0.3, 0.009] [datetime.date(1926, 7, 1), 0.09, -0.22, -0.3, 0.009] [datetime.date(1926, 7, 1), 0.09, -0.22, -0.3, 0.009] [datetime.date(1926, 7, 1), 0.09, -0.22, -0.3, 0.009] [datetime.date(1926, 7, 1), 0.09, -0.22, -0.3, 0.009] [datetime.date(1926, 7, 1), 0.09, -0.22, -0.3, 0.009] [datetime.date(1926, 7, 1), 0.09, -0.22, -0.3, 0.009] [datetime.date(1926, 7, 1), 0.09, -0.22, -0.3, 0.009] [datetime.date(1926, 7, 1), 0.09, -0.22, -0.3, 0.009] [datetime.date(1926, 7, 1), 0.09, -0.22, -0.3, 0.009]

All good stuff. Now to utilize within my professors detailed instructions. lol
Reply


Messages In This Thread
notation - by MCL169 - Apr-14-2023, 01:49 AM
RE: notation - by Larz60+ - Apr-14-2023, 02:17 AM
RE: notation - by deanhystad - Apr-14-2023, 03:29 AM
RE: notation - by MCL169 - Apr-14-2023, 04:46 AM
RE: notation - by buran - Apr-14-2023, 06:30 AM
RE: notation - by MCL169 - Apr-14-2023, 10:14 AM
RE: notation - by DeaD_EyE - Apr-14-2023, 10:45 AM
RE: notation - by MCL169 - Apr-14-2023, 12:02 PM
RE: notation - by MCL169 - Apr-14-2023, 12:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Forcing matplotlib to NOT use scientific notation when graphing sawtooth500 4 672 Mar-25-2024, 03:00 AM
Last Post: sawtooth500
  ''.join and start:stop:step notation for lists ringgeest11 2 2,596 Jun-24-2023, 06:09 AM
Last Post: ferdnyc
  issue with converting a scientific notation to standard notation thomaswfirth 4 1,661 Jun-06-2023, 06:06 PM
Last Post: rajeshgk
  Issue in writing sql data into csv for decimal value to scientific notation mg24 8 3,497 Dec-06-2022, 11:09 AM
Last Post: mg24
  Graphics Formatting - X-axis Notation and Annotations - Matplotlib silviover_junior 0 1,923 Mar-17-2021, 01:19 PM
Last Post: silviover_junior
  How to understand the byte notation in python3 blackknite 3 3,090 Feb-23-2021, 04:45 PM
Last Post: bowlofred
  Simple question concerning python dot notation. miner_tom 1 2,023 Mar-24-2020, 05:20 PM
Last Post: buran
  how to implement the .mymethod() notation of Python Pedroski55 4 3,747 Apr-22-2019, 10:24 PM
Last Post: Gribouillis
  Object madness - JSON Notation confusion execsys 4 3,899 May-03-2018, 08:56 AM
Last Post: buran
  printing engineering notation Skaperen 1 5,379 Sep-28-2017, 03:40 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020