使用Python读取文件标题和分隔符 [英] Read File headers and delimiters using Python

查看:1491
本文介绍了使用Python读取文件标题和分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取指定文件夹中的所有文件(包含Dir,Sub dir和类型为.csv,.txt ..的文件)

I am reading all the files from a given folder (contains Dir, Sub dir and files of type .csv, .txt ..)

以下信息以以下格式输出到输出文件中:

I need to get the following information into an output file in the following format:

FileLocation, FileName, Delimiter, Columns 

(用分隔符分隔的单元格中需要的所有列)

(All columns needed in a cell separated by delimiter)

我使用下面的脚本,除了分隔符,它工作正常。我试过使用csv.sniffer,但它不工作。

I am using the following script which works fine except delimiter. I have tried using csv.sniffer but it does not work.

import sys,os,csv

ofilew = open('D:\OutputFile/Columns_Info.csv', 'w')
ofile = open('D:\OutputFile/Columns_Info.csv', 'a')

root = 'D:\UnZipFiles'
path = os.path.join(root)

columninfo = 'FolderLocation, FileName, Delimiter, Columns' + '\n'
ofilew.write(columninfo)

for r,d,f in os.walk(path):
    for file in f:
        fullfilepath = os.path.join(r,file)
        with open(fullfilepath,'r') as f:
            columninfo = f.readline()
            columninfo = columninfo.replace(",", ";")

            output = file +','+ columninfo
            outputfinal = r + ',' + output

            ofile.write(outputfinal)


推荐答案

以下方法适用于您,它使用Python的 csv.sniffer 功能尝试确定正确的方言用于读取文件。这也包含使用的分隔符。

The following approach should work for you, it uses Python's csv.sniffer feature to attempt to determine the correct dialect to use for reading the file. This also contains the delimiter that is used.

import os, csv

header_output = ['FolderLocation', 'FileName', 'Delimiter', 'Columns']
path = r'D:\UnZipFiles'

with open(r'D:\OutputFile\Columns_Info.csv', 'wb') as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerow(header_output)

    for root, folders, files in os.walk(path):
        for file in files:
            full_file_path = os.path.join(root, file)

            with open(full_file_path, 'rb') as f_input:
                try:
                    dialect = csv.Sniffer().sniff(f_input.read(1024))
                    f_input.seek(0)
                    csv_input = csv.reader(f_input, dialect)
                    header_input = next(csv_input)
                    csv_output.writerow([root, file, dialect.delimiter] + header_input)
                except csv.Error as e:
                    print "{} - could not determine the delimiter".format(file)

csv.sniffer ,你可以设计自己的,但Python比这更强大:

As an alternative to csv.sniffer, you could devise your own, but the Python one is much more powerful than this:

def get_delimiter(file_name):            
    cols_found = []

    for delim in [',', ';', '|', '\t']:
        with open(file_name, 'rb') as f_in:
            cols_found.append([len(next(csv.reader(f_in, delimiter=delim))), delim])

    if cols_found[-1][0] > 1:
        return sorted(cols_found)[-1][1]
    else:
        return None


print get_delimiter('my.csv')

这通过计算哪个分隔符产生第一行中最多的列,返回一个可能的分隔符。如果只找到一个列,它返回 None ,表示找不到匹配的分隔符。它可能会引发异常。

This returns a possible delimiter by counting which delimiter results in the most columns in the first row. If only one column is found, it returns None to indicate no matching delimiter was found. It could instead raise an exception.

这篇关于使用Python读取文件标题和分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆