Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
notation
#1
for line in lines:                                          # str, '19260701,0.09,-0.22,-0.30,0.009\n'
    curr_line = line.split()                                # list, ['19260701', '0.09', '-0.22, '-0.30', '0.009']
    my_date = curr_line[0]                              # str, '19260701' 
My question is about the notation
for "my_date = curr_line[0]" is it "# str, '19260701'" or #str, '19260701', '0.09', '-0.22, '-0.30', '0.009'

I believe curr_line[0] is 0 index for first string in first list ( '19260701') but im not sure.
Reply
#2
an easy to see where something like this is occurring, is to run the code with the debugger.

You can also run manually to see what's going on:
>>> line1 = '19260701,0.09,-0.22,-0.30,0.009\n'
>>> line1.strip().split()
['19260701,0.09,-0.22,-0.30,0.009']
>>>
Reply
#3
curr_line[0] is not "19260701" or 19260701', '0.09', '-0.22, '-0.30', '0.009'. It is "19260701,0.09,-0.22,-0.30,0.009\n" because there are no spaces in the str. Maybe you should use split(",")
line = "19260701,0.09,-0.22,-0.30,0.009\n"
print(line.strip().split(","))
Output:
['19260701', '0.09', '-0.22', '-0.30', '0.009']
Reply
#4
If split creates a lists of strings is [0] the first line in the file or the first item in a list?
Reply
#5
(Apr-14-2023, 04:46 AM)MCL169 Wrote: If split creates a lists of strings is [0] the first line in the file or the first item in a list?
What is lines in the first place?
Anyway, in case like you can simply add some print calls to see for yourself. And actually the comments suggest you already did that, so your question (or confusion) is unclear.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(Apr-14-2023, 06:30 AM)buran Wrote:
(Apr-14-2023, 04:46 AM)MCL169 Wrote: If split creates a lists of strings is [0] the first line in the file or the first item in a list?
What is lines in the first place?
Anyway, in case like you can simply add some print calls to see for yourself. And actually the comments suggest you already did that, so your question (or confusion) is unclear.

Yeah I'm sorry it might seem like I'm just going through the motions but what it is my professor is very anal about stuff like that and I just was not sure how to notate that particular line of code but it is running correctly.
Reply
#7
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]
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#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
#9
for line in lines: # str, '19260701,0.09,-0.22,-0.30,0.009\n'
21 curr_line = line.split() # list, ['19260701', '0.09', '-0.22, '-0.30', '0.009']
22 my_date = curr_line[0] # str, list
### recheck this notation
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Forcing matplotlib to NOT use scientific notation when graphing sawtooth500 4 401 Mar-25-2024, 03:00 AM
Last Post: sawtooth500
  ''.join and start:stop:step notation for lists ringgeest11 2 2,445 Jun-24-2023, 06:09 AM
Last Post: ferdnyc
  issue with converting a scientific notation to standard notation thomaswfirth 4 1,386 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,078 Dec-06-2022, 11:09 AM
Last Post: mg24
  Graphics Formatting - X-axis Notation and Annotations - Matplotlib silviover_junior 0 1,793 Mar-17-2021, 01:19 PM
Last Post: silviover_junior
  How to understand the byte notation in python3 blackknite 3 2,934 Feb-23-2021, 04:45 PM
Last Post: bowlofred
  Simple question concerning python dot notation. miner_tom 1 1,918 Mar-24-2020, 05:20 PM
Last Post: buran
  how to implement the .mymethod() notation of Python Pedroski55 4 3,589 Apr-22-2019, 10:24 PM
Last Post: Gribouillis
  Object madness - JSON Notation confusion execsys 4 3,699 May-03-2018, 08:56 AM
Last Post: buran
  printing engineering notation Skaperen 1 5,201 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