Julliette
Julliette

Reputation: 27

read excel file with words and write it to csv file in Python

I have an excel file with string stored in each cell:

rtypl   srtyn   OCVXZ   srtyn
KPLNV   KLNWZ   bdfgh   KLNWZ
xcvwh   mvwhd   WQKXM   mvwhd
GYTR    xvnm    YTZN    YTZN
ngws    jklp    PLNM    jklp

I wanted to read excel file and write it in csv file. As you can see below:

import pandas as np
import csv

df = pd.read_excel(file, encoding='utf-16')
words= open("words.csv",'wb')
wr = csv.writer(words, dialect='excel')
for item in df:
    wr.writerow(item)

But it reads the each line in separated alphabet and not as a string.

r,t,y,p,l

I am limited to write file as csv as I gonna use the result in a library that has lots of facility for csv file. Any advice on how I can read all the rows as a string in the cell is appreciated.

Upvotes: 0

Views: 2601

Answers (2)

zipa
zipa

Reputation: 27869

You can try the easiest solution:

# -*- coding: utf-8 -*-
import pandas as pd

df = pd.read_excel(file, encoding='utf-16')
df.to_csv('words.csv', encoding='utf-16')

Upvotes: 2

San
San

Reputation: 161

Adding to zipa : If excel has multiple sheets : you can also try

import pandas as pd

df = pd.read_excel(file, 'Sheet1')
df.to_csv('words.csv')

Refer : http://www.gregreda.com/2013/10/26/intro-to-pandas-data-structures/

Upvotes: 0

Related Questions