Posts: 21
Threads: 9
Joined: Jan 2023
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.
Posts: 12,031
Threads: 485
Joined: Sep 2016
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']
>>>
Posts: 6,800
Threads: 20
Joined: Feb 2020
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']
Posts: 21
Threads: 9
Joined: Jan 2023
Apr-14-2023, 04:46 AM
(This post was last modified: Apr-14-2023, 05:14 AM by MCL169.)
If split creates a lists of strings is [0] the first line in the file or the first item in a list?
Posts: 8,160
Threads: 160
Joined: Sep 2016
Apr-14-2023, 06:30 AM
(This post was last modified: Apr-14-2023, 06:30 AM by buran.)
(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.
Posts: 21
Threads: 9
Joined: Jan 2023
(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.
Posts: 2,126
Threads: 11
Joined: May 2017
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]
Posts: 21
Threads: 9
Joined: Jan 2023
(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
Posts: 21
Threads: 9
Joined: Jan 2023
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
|