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")
If values of your data includes commas you have several options to store them:
- 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='\')
- 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.
- 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)
Tags: csv, exception, pythonpython