简体   繁体   中英

Reformatting a user-input

I want to create a program which will reformat a string inputted by a user into a more acceptable string.

The string should be in this format:

a list of authors (year) title, other information

For example if a user inputs:

"suleman, h (2001) Some Random Piece Of Junk, Journal of Junk, ACM, pp1-100, 2000"

my program should return:

"Suleman, H (2001) Some random piece of junk, Journal of Junk, ACM, pp1-100, 2000."

Here's a working solution, based on the structure of the input string:

str="suleman, h (2001) Some Random Piece Of Junk, Journal of Junk, ACM, pp1-100, 2000"

parts=str.split(',')
newparts=[]

for chunk in parts[:-3]:
    newparts.append(chunk.title())

newparts.append(parts[3].upper())

for chunk in parts[4:]:
    newparts.append(chunk.lower())


newstr=",".join(newparts)

print newstr

Result of run:

11:41 ~ $ python test.py
Suleman, H (2001) Some Random Piece Of Junk, Journal Of Junk, ACM, pp1-100, 2000

I originally thought that .title() would have provided you with the capitalization you needed, but you have terms which need to be fully cap'd, like ACM, and parts that need to be fully lowercase'd, like 'pp1-100'. So I took the more structured approach. Note, this will not work with a compound reference. I suspect there must be a specific python library to handle proper capitalization for a reference. However, as the rules can be complex and insertions not always clean, proper processing cannot be guaranteed.

The approach I have detailed above assumes that the last three comma separated terms are special cases, but everything before follows the TitleCase rules.

Suggestion:

Defensively code the input fields so that the information you want to be capitalized in a certain way are grouped together. Ie, TitleCase fields, ALL CAPS fields, ALL LOWER fields, and LEAVE-AS-IS fields.

Then process accordingly in code.

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