简体   繁体   中英

Python routine to abbreviate sentences?

I am working with CSV data from Survey Monkey. The problem is that the column names they use are way too long for most database CSV data import routines. The column names are, literally, the question you asked on the survey. So it can take some time to be able to narrow that down to, say, 64 characters for a database column name. What would a python routine look like to narrow down a question to a few words and/or characters? I can't just do something like remove the vowels since that would still be too long in most cases. Thoughts?

If you just need the headers to be unique and less than 64 (are you using postgres btw?) then just use a hash.

from hashlib import md5

columns = ['reallylongcolumname1', 'reallylongcolumname2']
out = {}
for c in columns:
    h = md5(c).hexdigest()
    # Use 'h' as your new column header!
    out.update({c : h})

save the dictionary "out" somewhere so you can decrypt this later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM