سؤال

I need to write a program which opens some rar files that contain csv files and read them. I know that there are external libraries for this purpose but what is the best way or library to achieve such a task ?

هل كانت مفيدة؟

المحلول

Python comes with batteries included. csv is one of these batteries.

Support for RAR can be added by an external library.

نصائح أخرى

As stated there is a RAR library for python. That being said you'll still need the unrar program installed on your computer. Using a mac you can install this via homebrew:

brew install unrar

With this installed, your python script might look something like the following:

import rarfile, csv
rar_path = rarfile.RarFile("/path/to/rar_file.rar")
csv_file_name = "rar_file.csv"
rar_file = rarfile.RarFile.open(rar_path, csv_file_name)
csv_reader = csv.reader(rar_file, delimiter=',')

# Should output first line of file (typically CSV header)
print csv_reader.next()
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top