Created
January 31, 2015 13:50
-
-
Save cnDelbert/e09b7281802d82ccac8f to your computer and use it in GitHub Desktop.
Format references to the given format.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
__author__ = 'Delbert' | |
import re | |
import xlwt | |
def parse(refer, formated): | |
r_file = open(refer, 'rt', encoding='utf-8') | |
save_file = open('saved.txt', 'wt', encoding='utf-8') | |
md_file = open('refFormat.md', 'wt', encoding='utf-8') | |
form = formated + '\n' | |
names = list() | |
papers = list() | |
journals = list() | |
others = list() | |
names_short = list() | |
years = list() | |
vols = list() | |
pages = list() | |
for line in r_file: | |
# remove line break | |
line = line.strip('\n') | |
# sn = re.compile('(\d+)\W*') | |
# remove numbers | |
p = re.compile("\(\d+\)\s") | |
r = p.match(line) | |
if r: | |
line = line.replace(r.group(0), '', 1) | |
# replace ; with , | |
line = line.replace(';', ',') | |
# replace comma with comma+space | |
p = re.compile('.+\S,\w+') | |
r = p.match(line) | |
if r: | |
# print(r.group(0)) | |
line = line.replace(',', ', ') | |
p = re.compile('.+[A-Z]\.[,A-Z]+') | |
r = p.match(line) | |
if r: | |
line = line.replace(r.group(), r.group().replace(',', '').replace('. ', ', ').replace('.', '').strip() + ',') | |
line = line.replace(' ', ' ') | |
names.append(line.split('.')[0].strip().strip(',')) | |
papers.append(line.split('.')[1].strip()) | |
try: | |
journals.append(line.split('.')[2].strip()) | |
except IndexError: | |
return | |
others.append('.'.join(line.strip('.').split('.')[3:]).strip().split(';')[0]) | |
if len(names[len(names) - 1].split(',')) > 6: | |
names_short.append(','.join(names[len(names) - 1].split(',')[:3]) + ', _et al_') | |
else: | |
names_short.append(names[len(names) - 1]) | |
years.append(line.split(';')[0].strip()[-4:]) | |
vols.append(line.split(';')[1].strip().split(':')[0].strip()) | |
pages.append(line.split(':')[-1].strip('.')) | |
print(line) | |
save_file.write(line+'\n') | |
r_file.close() | |
save_file.close() | |
ref_excel = xlwt.Workbook() | |
ref_sheet = ref_excel.add_sheet('reference sheet', cell_overwrite_ok=True) | |
for i in range(len(names)): | |
ref_sheet.write(i, 0, names[i]) | |
ref_sheet.write(i, 1, papers[i]) | |
ref_sheet.write(i, 2, journals[i]) | |
ref_sheet.write(i, 3, others[i]) | |
ref_sheet.write(i, 4, names_short[i]) | |
ref_excel.save('ref_sheet.xls') | |
# print(names) | |
# print(papers) | |
# print(journals) | |
# print(others) | |
# ref_excel = xlrd.open_workbook('ref_sheet.xls') | |
# ref_sheet = ref_excel.sheet_by_name('reference sheet') | |
for i in range(len(names)): | |
md_file.write(form.format(number=i+1, name=names_short[i], paper=papers[i], journal=journals[i], | |
other=others[i], year=years[i], vol=vols[i], page=pages[i])) | |
md_file.close() | |
def test(): | |
string = '(53) Bushby KM,Hill A,Steele JG ,Failure of early diagnosis in symptomatic Duchenne muscular dystrophy. Lancet.1999V353N9152:557-8' | |
p = re.compile("\(\d+\)\s") | |
r = p.match(string) | |
print(r.group(0)) | |
def main(): | |
form = '{number}. {name}. {paper}. _{journal}_. {year}; __{vol}__:{page}.' | |
# form = '- {name}. {paper}. _{journal}_. {year}; __{vol}__:{page}.' | |
parse('ref.txt', form) | |
# test() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment