使用python将所有csv文件从编码ansi转换为utf8

时间:2013-12-12 07:58:34

标签: python csv utf-8

我有如下的python代码:

import os
from os import listdir

def find_csv_filenames( path_to_dir, suffix=".csv" ):
    filenames = listdir(path_to_dir)
    return [ filename for filename in filenames if filename.endswith( suffix ) ]
    #always got the error this below code
filenames = find_csv_filenames('C:\casperjs\project\teleservices\csv')
for name in filenames:
    print name

我遇到了错误:

filenames = find_csv_filenames('C:\casperjs\project\teleservices\csv')
Error message: `TabError: inconsistent use of tabs and spaces in indentation`

我需要什么:我想读取所有csv文件并将其从编码ansi转换为utf8,但上面的代码只是每个csv文件的读取路径。我不知道它有什么问题?

3 个答案:

答案 0 :(得分:1)

下面将转换ascii-file中的每一行:

import os
from os import listdir

def find_csv_filenames(path_to_dir, suffix=".csv" ):
    path_to_dir = os.path.normpath(path_to_dir)
    filenames = listdir(path_to_dir)
    #Check *csv directory
    fp = lambda f: not os.path.isdir(path_to_dir+"/"+f) and f.endswith(suffix)
    return [path_to_dir+"/"+fname for fname in filenames if fp(fname)]

def convert_files(files, ascii, to="utf-8"):
    for name in files:
        print "Convert {0} from {1} to {2}".format(name, ascii, to)
        with open(name) as f:
            for line in f.readlines():
                pass
                print unicode(line, "cp866").encode("utf-8")    

csv_files = find_csv_filenames('/path/to/csv/dir', ".csv")
convert_files(csv_files, "cp866") #cp866 is my ascii coding. Replace with your coding.

答案 1 :(得分:0)

请参阅文档:http://docs.python.org/2/howto/unicode.html

如果你需要一个字符串,比如它存储为 s ,你想要编码为特定的格式,你可以使用s.encode()

答案 2 :(得分:0)

您的代码只是列出csv文件。它没有做任何事情。如果需要阅读,可以使用csv模块。如果您需要管理编码,可以执行以下操作:

import csv, codecs
def safe_csv_reader(the_file, encoding, dialect=csv.excel, **kwargs):
    csv_reader = csv.reader(the_file, dialect=dialect, **kwargs)
    for row in csv_reader:
        yield [codecs.decode(cell, encoding) for cell in row]

reader = safe_csv_reader(csv_file, "utf-8", delimiter=',')
for row in reader:
    print row