How to check downloaded csv or xls file in python?

How to check if a CSV or XLS file is uploaded . How to check this in python. I am importing a file into a binary field in openerp, which can be restored as a binary. I need to read a file and import data into a table. User can download csv or xls file. Knowing only, I can use the csv or xlrd package.

+4
source share
2 answers

The sixth signature for the file is.xls as follows:

Excel Spreadsheet Subtitle (MS Office)

09 08 10 00 00 06 05 00 [512 byte offset]

You can read about the various other signatures on Wikipedia .

, - . , , . , . !

xls_sig = b'\x09\x08\x10\x00\x00\x06\x05\x00'
offset = 512
size = 8

with open('spreadsheet.xls', 'rb') as f:
    f.seek(offset)       # Seek to the offset.
    bytes = f.read(size) # Capture the specified number of bytes.

    if bytes == xls_sig:
        print 'Uploaded file is an xls.'
    else:
        print 'File is not an xls.'

1

, , .xls.

2

, , xls xlsx:

import codecs

xlsx_sig = b'\x50\x4B\x05\06'
xls_sig = b'\x09\x08\x10\x00\x00\x06\x05\x00'

filenames = [
    ('spreadsheet.xls', 0, 512, 8),
    ('spreadsheet.xlsx', 2, -22, 4)]

for filename, whence, offset, size in filenames:
    with open(filename, 'rb') as f:
        f.seek(offset, whence) # Seek to the offset.
        bytes = f.read(size)   # Capture the specified number of bytes.

        print codecs.getencoder('hex')(bytes)

        if bytes == xls_sig:
            msg = '"{}" is an xls.'
        elif bytes == xlsx_sig:
            msg = '"{}" is an xlsx.'
        else:
            msg = '"{}" is not an Excel document.'
        print msg.format(filename)

:

('0908100000060500', 8)
"spreadsheet.xls" is an xls.
('504b0506', 4)
"spreadsheet.xlsx" is an xlsx.
+5

, , .

import xlrd
import csv

try:
    # reading the file by xlrd
    ...
    print "Thanks for your Excel file"
except: # if you find specific Exception types, use them here
    try:
        # reading as CSV file
        ...
        print "thanks for your CSV file"
    except: # if you find specific Exception types, use them here
        print "sorry, now way, give me some usable file."
+1

Source: https://habr.com/ru/post/1539554/


All Articles