Home » Python » python – How do I make sure when I save data to a csv it keeps the commas?-Exceptionshub

python – How do I make sure when I save data to a csv it keeps the commas?-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I am try to pre-process some data while trying to learn machine learning. Right now im stuck as the data I want to save, I need to have the commas included in the saved csv file. How would I go along doing that

for x in X:
    for z in x:
        print(z)
        Z.append(z)
    V.append(Z)
    Z = []
    if i == 1:
        break
pd.DataFrame(V).to_csv("foo.csv")
How to&Answers:

If values of your data includes commas you have several options to store them:

  1. Use escapechar to escape them.
for x in X:
    for z in x:
        print(z)
        Z.append(z)
    V.append(Z)
    Z = []
    if i == 1:
        break
pd.DataFrame(V).to_csv("foo.csv", escapechar='\')

Don’t forget to use it when loading the same file to avoid confusion

df = pd.load_csv("foo.csv", escapechar='\')
  1. Use another delimiter than a coma, for example a tab \t or pipe |
for x in X:
    for z in x:
        print(z)
        Z.append(z)
    V.append(Z)
    Z = []
    if i == 1:
        break
pd.DataFrame(V).to_csv("foo.csv", sep='\t')

Please refer to documentation for all related options.

  1. Moreover, you don’t have to use Pandas for such simple task as writing CSV files. You can use csv module. Examples from the tutorial. This option is more complicated in the code, but more efficient as well.
...
with open('foo.csv', 'w') as f:
    writer = csv.writer(f)
    for row in V:
        writer.writerow(row)

You can register own dialect to use custom settings, for example:


csv.register_dialect("hashes", delimiter="#")
with open('foo.csv', 'w') as f:
    writer = csv.writer(f, dialect="hashes")
    for row in V:
        writer.writerow(row)