Created
December 2, 2020 02:03
-
-
Save yencarnacion/6c054ba930b959e324e0bdac222935cf to your computer and use it in GitHub Desktop.
Examples of writing a csv file in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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