简体   繁体   中英

Exporting pandas dataframe to CSV

I'm loading a SQL table into a dataframe, and then pushing it directly into a CSV. The Problem is the export. I require:

value|value|value

and I'm getting:

"(value|value|value)"

How do I get out of that?

Here's my code:

for row in self.roster.itertuples():
    SQL = self.GenerateSQL(row)
    self.filename = '{}_{}.csv'.format(row.tablename, now.strftime("%Y-%m-%d"))
    # Open the file
    f = open(os.path.join(self.path, self.filename), 'w')
    # Create a connection and get a cursor
    cursor = self.conn.cursor()
    # Execute the query
    cursor.execute(SQL)
    # Get data in batches
    rowcount = 0
    while True:
        # Read the data
        df = pd.DataFrame(cursor.fetchmany(1000))
        # We are done if there are no data
        if len(df) == 0:
            break
        # Let's write to the file
        else:
            rowcount += len(df.index)
            print('Number of rows exported: {}'.format(str(rowcount)))
            df.to_csv(f, header=False, sep='|', index=False)

    # Clean up
    f.close()
    cursor.close()

Appreciate any insight.

UPDATE #1 This is an output of the df during the 1000 record cycles.

[1000 rows x 1 columns]
Number of rows exported: 10000
                                                     0
0    [11054, Smart Session (30 Minute) , smartsessi...
1    [11055, Best Practices, bestpractices, 2018-06...
2    [11056, Smart Session (30 Minute) , smartsessi...
3    [11057, Best Practices, bestpractices, 2018-06...

two records:

                                                   0
0  [1, Offrs.com Live Training, livetraining, 201...
1  [2, Offrs.com Live Training, livetraining, 201...

Provided that you can use sqlalchemy package, you would be able to take advantage of the pd.read_sql function which handles querying the database and retrieving the data.

import pandas as pd
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)

from sqlalchemy import create_engine
engine = create_engine('postgresql://postgres@localhost:5432/sample')

df = pd.read_sql_query('select * from climate limit 3',con=engine)
df.to_csv('out.csv', header=False, sep='|', index=False)

Alternatively, you can still use the cursor. However, you need to split the rows fetched into individual pieces before constructing a data frame. Currently, the whole row with multiple database table columns is put into a single dataframe row.

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