If you want to assign csv export to a variable in python

Use io.StringIO

from io import StringIO                                                                                     
import csv                                                                                                  
                                                                                                            
f = StringIO()                                                                                              
writer = csv.writer(f, quotechar='"', quoting=csv.QUOTE_ALL, lineterminator="\n")                           
writer.writerow(['foo,', 'bar'])                                                                             
writer.writerow(['"Ai', 'Ueo\''])                                                                      
result = f.getvalue()                                                                                       
print(type(result))                                                                                         
print(result)  

csvfile parameter specifications

According to Document, the following parameters can be specified. csv.writer(csvfile[, dialect='excel'][, fmtparam]) It seems that the csvfile object can be any object that has a write method. Therefore, sys.stdout etc. can also be specified.

I did a little research from the request that I want to be able to copy and paste on the WEB instead of exporting as CSV and saving it in a file. I found how to use StringIO in the csv module test (Python-3.5.2 / Lib / test / test_csv.py).

Recommended Posts

If you want to assign csv export to a variable in python
I want to embed a variable in a Python string
Check if you can connect to a TCP port in Python
How to embed a variable in a python string
If you want to create a Word Cloud.
If you want a singleton in python, think of the module as a singleton
I want to convert a table converted to PDF in Python back to CSV
If you want to count words in Python, it's convenient to use Counter.
I want to easily implement a timeout in python
If you want to make a TODO application (distributed) now using only Python
If you want to display values using choices in a template in a Django model
I want to write in Python! (2) Let's write a test
I want to randomly sample a file in Python
I want to work with a robot in python.
If you encounter a "Unicode Decode Error" in Python
When you want to plt.save in a for statement
If you want to make a discord bot with python, let's use a framework
Understand Python yield If you put yield in a function, it will change to a generator
[Python] If you suddenly want to create an inquiry form
Why do you add a main ()-if statement in Python?
How to import a file anywhere you like in Python
A story about trying to implement a private variable in Python.
I want to make input () a nice complement in python
When you want to hit a UNIX command on Python
If you want to use field names with hyphens when updating firestore data in python
[Subprocess] When you want to execute another Python program in Python code
Do you want to wait for general purpose in Python Selenium?
If you want to become a data scientist, start with Kaggle
Don't write Python if you want to speed it up with Python
I want to know if you install Python on Mac ・ Iroha
I want to print in a comprehension
How to get a stacktrace in python
Read Python csv and export to txt
[Python] The role of the asterisk in front of the variable. Divide the input value and assign it to a variable
When you want to replace multiple characters in a string without using regular expressions in python3 series
What to do if there is a decimal in python json .dumps
What to do if you can't use scikit grid search in Python
If you want to include awsebcli with CircleCI, specify the python version
If you want to get multiple statistics with groupby in pandas v1
I want to color a part of an Excel string in Python
What happens if you do "import A, B as C" in Python?
I want to do a monkey patch only partially safely in Python
If you want to switch the execution user in the middle of a Fabric task, settings context manager
[Python] What to do if you get a ModuleNotFoundError when importing pandas using Jupyter Notebook in Anaconda
Csv in python
How to read a CSV file with Python 2/3
I want to do Dunnett's test in Python
Try to calculate a statistical problem in Python
How to clear tuples in a list (Python)
A clever way to time processing in Python
Steps to develop a web application in Python
I want to merge nested dicts in Python
Decide to assign a laboratory with Python (fiction)
To add a module to python put in Julialang
How to notify a Discord channel in Python
Export Python3 version OpenCV KeyPoint to a file
[Python] How to draw a histogram in Matplotlib
I want to write to a file with Python
I want to display the progress in Python!
Various ways to read the last line of a csv file in Python
What to do if you get a "No versions found" error in pipenv