Skip to content

Instantly share code, notes, and snippets.

@sloev
Created April 21, 2020 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sloev/1625dd0020bb84140afcb2d2cb741ef1 to your computer and use it in GitHub Desktop.
Save sloev/1625dd0020bb84140afcb2d2cb741ef1 to your computer and use it in GitHub Desktop.
how to encode values to csv string in python 3 (args to csv string)
import csv
from io import StringIO
class ArgsToCsv:
def __init__(self, seperator=","):
self.seperator = seperator
self.buffer = StringIO()
self.writer = csv.writer(self.buffer)
def stringify(self, *args):
self.writer.writerow(args)
value = self.buffer.getvalue().strip("\r\n")
self.buffer.seek(0)
self.buffer.truncate(0)
return value + "\n"
from args_to_csv import ArgsToCsv
csv_formatter = ArgsToCsv()
output = ""
header = ["age", "text", "my_array"]
output += csv_formatter.stringify(*header)
output += csv_formatter.stringify(
10,
"""
lol i have some pretty
"freaky"
strings right here \' yo!
""",
[10, 20, 30],
)
output += csv_formatter.stringify(10, "hi there", None)
assert output.splitlines() == [
"age,text,my_array",
'10,"',
" lol i have some pretty",
' ""freaky""',
" strings right here ' yo!",
' ","[10, 20, 30]"',
"10,hi there,",
]
@sloev
Copy link
Author

sloev commented Jul 20, 2020

thanks for collaboration.

i think our usecases are quite different

i needed a way to on-the-fly encode args to strings as i was continously writing to a http socket (streaming csv lines back to client)

you seem to "just" want to encode stuff to csv to afterwards take the resulting string buffer and use it for some db action.

i think you could get away with this cheaper with just:

buffer = StringIO()
writer = csv.writer(buffer)
writer.writerow(*column_names)
for row in x:
    writer.writerow(*row)

cur.copy_from(buffer, table_name, sep=',')

no need to do classy stuff here

@rupestre-campos
Copy link

Hey man, thank's! You are right about the class, this way you propose to me is way more simple, and I'll sticky to it.

@harisreedhar
Copy link

@sloev Thanks for the snippet. I think you forgot to use separator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment