Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pattern recogintion
#11
You're trying to insert the file-name in the function definition.
This is wrong.

def do_something(my_file):
    print(my_file)
my_file is a name, which is accessible inside the function.
The name my_file points to the object, which is the first argument of the call.

do_something("a_file.csv")
#            ^^^^^^^^^^^^
#            First Argument, my_name
You have to call:
get_data("AAPL.csv", "AAPL_filtered.csv")
No def in front of the function call.
def stands for definition of function followed by the name and in the parenthesis the arguments and keyword arguments.
Everything which is in this block, is inside the function.

Calling a function, requires the name, the parenthesis and the arguments.
Some function don't require arguments, but in your case you need two arguments.
Old file, new file.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#12
okay, you are right, silly me...

well, i tried it - on the one hand it doesn't give any error, it simply return the next line of command in the terminal, but on the other hand it doesn't produce any file, or any results even if i have the file ready (created),

here is the code you gave me + the addition...

import csv
 
 
def condition(first, second):
    try:
        s_open = float(second["Open"])
        f_low = float(first["Low"])
        s_close = float(second["Close"])
        f_high = float(first["High"])
    except ValueError:
        # throw bad data away
        return False
    return s_open < f_low and s_close > f_high
 
 
def get_data(csv_file, new_csv):
    with open(csv_file, "rt", encoding="utf8", newline="") as fd_in:
        with open(new_csv, "wt", encoding="utf8", newline="") as fd_out:
            reader = csv.DictReader(fd_in)
            writer = csv.writer(fd_out)
            writer.writerow(reader.fieldnames)
            first_day, second_day = [iter(reader)] * 2
            # a little trick to get the same identical iterator twice
            for first, second in zip(first_day, second_day):
                if condition(first, second):
                    writer.writerow(first.values())
                    writer.writerow(second.values())

    get_data("AAPL.csv", "AAPL2.csv")
btw, here it shows the last line (at least now, that i'm viewing the preview of the message) with the 'get_data' without a lower line...but even if it's not seen, it is there...

btw, when i read your code everything is (more or less) crystal clear...
but how do i learn to create this kind of scripts myself ?

i read here, i read there, i understand the bits and pieces but how do i learn such combinations ? i think the actual combinations is what builds a program eventually, isn't it ?

also, what is rt and wt stand for ?
Reply
#13
https://docs.python.org/3/library/functions.html#open

ReadText
WriteText
The resulting datatype is str (unicode).

The Text-Mode is implicit. This means if you ommit the t, the file is still open in text mode.
If you need raw bytes, then you have to open the file explicit in binary mode. rb for reading and wb for writing. If you omit everything, just supply open(file), then the file is open in read text mode.

The Python developers made it easy for us. In the most cases you want to read strings and this is the default of the open function.

Depending on the keyword-argument encoding the source encoding is translated into unicode str. So if the file was encoded with latin1, you need to do it explicit. By default encoding utf8 is used, if you don't supply the function with this keyword-argument. If you have only ascii as encoding, you choose ascii.

In addition, you may get UnicodeDecodeError. Sometimes encodings from different sources are broken. In this case you can ignore this errors with the ignore keyword-argument, which is also described in the docs.

By the way, if you use the FireFox, you could install this addon: https://addons.mozilla.org/de/firefox/addon/pythondoc/
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#14
i see,

what about the script/program, what else has to be done in order to produce the output file ?
Reply
#15
I didn't quite understand yet how to make this script/program work...
Reply


Forum Jump:

User Panel Messages

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