简体   繁体   中英

Writing function output to text file

I am looking to write the contents of a pandas dataframe to a text file. This is a sample of the dataframe I am working with:

last_week = pd.date_range(start=(datetime.today() - timedelta(weeks=1)), end=datetime.today()).map(lambda x: x.strftime('%Y-%m-%d'))

df = pd.DataFrame(index=pd.MultiIndex.from_product([list(last_week), ['A', 'B', 'C']]),
                  data={'Category 1':np.random.randint(100, size=24),
                        'Category 2':np.random.randint(100, size=24)})

df.index = df.index.set_levels([pd.to_datetime(df.index.levels[0], format='%Y-%m-%d'), df.index.levels[1]])
df.index.set_names(['Day', 'App Name'], level=[0,1], inplace=True)

I have written a function that outputs the dataframe contents in my desired format:

def write_to_file():
    for n in df.reset_index().index:
        row = df.reset_index().iloc[n]
        row = row[row.notnull()]

        print(f"""
\tDay: {datetime.strftime(row.loc['Day'], '%Y-%m-%d')}
\tApp: {row.loc['App Name']}""")

        for i in row.index[2:]:
            print(f"\t{i}: {round(row.loc[i], 2)}")

When I call this function it outputs the following:

    Day: 2020-02-13
    App: A
    Category 1: 6
    Category 2: 73

    Day: 2020-02-13
    App: B
    Category 1: 39
    Category 2: 41

    Day: 2020-02-13
    App: C
    Category 1: 15
    Category 2: 15

    Day: 2020-02-14
    App: A
    Category 1: 26
    Category 2: 8

    Day: 2020-02-14
    App: B
    Category 1: 72
    Category 2: 8

    Day: 2020-02-14
    App: C
    Category 1: 62
    Category 2: 32

    Day: 2020-02-15
    App: A
    Category 1: 76
    Category 2: 21

    Day: 2020-02-15
    App: B
    Category 1: 35
    Category 2: 19

However, when I try to write the function output to a file ...

with open('./filepath', 'w') as f:
    f.write(write_to_file())

it doesn't work because the function output is Nonetype. Can anyone suggest a method for writing this output to a file?

Thanks!

The reason your f.write() doesn't work is because write_to_file() outputs using print() . print() writes to what we call "standard output" or stdout for short. By default, stdout is the the terminal where you run your program.

Operating systems allow you to redirect stdout to a file. You do this with a command like the following:

python program.py > output.txt

When you do this, you won't see any output in your console. Instead, you can open the output.txt file after the program finishes running and see the output there.

This solution has the advantage that it works without any changes to your Python code. It is also a common standard for command-line programs. Alternatively, you can write to a file from your python code. This requires opening a file, as you know how to do. Then you have to call f.write() instead of print() :

def write_to_file():
    with open('output.txt') as f:
        for n in df.reset_index().index:
            row = df.reset_index().iloc[n]
            row = row[row.notnull()]

            f.write(f"""
\tDay: {datetime.strftime(row.loc['Day'], '%Y-%m-%d')}
\tApp: {row.loc['App Name']}""")

            for i in row.index[2:]:
                f.write(f"\t{i}: {round(row.loc[i], 2)}")

To make this function more flexible, you can add a parameter for the file name, instead of always writing to output.txt . This change is left as an exercise for the reader.

Instead of printing your output in write_to_file() , you need to return a string so you can output that to file. If it is potentially a long string, see this article for efficient string concatenation in python.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM