Skip to content

Instantly share code, notes, and snippets.

@yencarnacion
Created December 2, 2020 02:03
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 yencarnacion/6c054ba930b959e324e0bdac222935cf to your computer and use it in GitHub Desktop.
Save yencarnacion/6c054ba930b959e324e0bdac222935cf to your computer and use it in GitHub Desktop.
Examples of writing a csv file in python
a_list = ['uno, dos', 'tres, cuatro']
for x in a_list:
out = "%s\n" % (x)
# puede que en Windows tengas que hacer lo que sigue
# out = "%s\r\n" % (x)
with open("outfile1.csv","a") as fo:
fo.write(out)
# usando csv
import csv
for x in a_list:
out = x.split(",")
with open("outfile2.csv", 'a') as fo:
writer = csv.writer(fo, delimiter=",")
writer.writerow(out)
# si tu data está en un dataframe puedes usar
import pandas as pd
df = pd.DataFrame(data={"col1": ['uno', 'tres'], "col2": ['dos', 'cuatro']})
df.to_csv("./outfile3.csv", sep=',',index=False, header=None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment