1

I have problems in converting several files that is at .dta to .csv all at once using panda in python, could you help me on how to go about this because I have different files in like four folders that all contain .dta files?

asked May 28, 2015 at 15:24
1
  • 3
    Please show your efforts, it's unclear where you are stuck at Commented May 28, 2015 at 15:29

1 Answer 1

2

The pandas.io module has a read_stata function: http://pandas.pydata.org/pandas-docs/dev/generated/pandas.io.stata.read_stata.html.

That will read an individual stata file into a dataframe. From there you can use the dataframe's .to_csv method to save a new file in your desired format.

When it comes to getting all of the data in your directories, I think your quickest path forward will look something like this (untested):

import glob
import os
import pandas
my_directories = ['/path/to/first', '/path/to/second', ..., '/path/to/nth']
for my_dir in my_directories:
 stata_files = glob.glob(os.path.join(my_dir, '*.dta')) # collects all the stata files
 for file in stata_files:
 # get the file path/name without the ".dta" extension
 file_name, file_extension = os.path.splitext(file)
 # read your data
 df = pandas.read_stata(file, ...)
 # save the data and never think about stata again :)
 df.to_csv(file_name + '.csv')
answered May 28, 2015 at 17:41
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.