Converting Refer/BibIX to Excel format

As I promised, I come up with a clean code for converting the Refer/BibIX ( also Zotoro exported collection ) to a clear Excel format.

#! /usr/bin/env python

import sys
import os

def index_2d(myList, v):
	output = ""
	for i, x in enumerate(myList):
		if v in x:
			output += x[3:]
	return output

txt = ""
xls = ""

directory = sys.argv[1]

if len(sys.argv) < 1:
	sys.exit('Usage: ./zotoro.py directory')

if not os.path.exists(directory): 
	exit('Directory not found!.\nUsage: ./zotoro.py directory')

files = os.listdir(directory)

for theFile in files:
	with open(directory+'/'+theFile) as infile:
		for line in infile:
			txt += line

TagsMap = [[0,'%0','ReferenceType'], [1,'%A','Author'], [2,'%T','Title'], [3,'%8','Date'], [4,'%B','BookTitle'], 
[5,'%D','Year'], [6,'%N','Issue'], [7,'%P','Pages'], [8,'%U','URL'], [9,'%V','Volume'], [10,'%X','Abstract'], 
[11,'%I','Publisher'], [12,'%J','JournalName'], [13,'%K','Keywords'], [14,'%E','Editor'], [15,'%F','Label'], 
[16,'%C','PlacePublished'], [17,'%S','TertiaryTitle'], [18,'%Y','Translator'], [19,'%Z','Notes'], 
[20,'%6','NumberofVolumes'], [21,'%L','CallNumber'], [22,'%M','AccessionNumber'], [23,'%7','Edition'], 
[24,'%9','TypeofWork'], [25,'%?','SubsidiaryAuthor'], [26,'%@','ISBN|ISSN'], [27,'%!','ShortTitle'], 
[28,'%[','OriginalPublication'], [29,'%]','ReprintEdition'], [30,'%*','ReviewedItem'], [31,'%+','AuthorAddress'], 
[32,'%^','Caption'], [33,'%1','Custom1'], [34,'%2','Custom2'], [35,'%3','Custom3'], [36,'%4','Custom4'], 
[37,'%#','Custom5'], [38,'%$','Custom6'], [39,'%&','Section']]

Records = txt.split('\r\n\r\n')

# adding the titles of the columns
for i,tag,title in TagsMap:
	if i<len(TagsMap)-1: xls += TagsMap[i][2] + '\t'
	else: xls += TagsMap[i][2] + '\n'

for record in Records:
	Fields = record.split('\r\n')
	for i,tag,title in TagsMap:
		if i<len(TagsMap)-1: xls += index_2d(Fields,tag) + '\t'
		else: xls += index_2d(Fields,tag) + '\n'

f = open(directory+'/output.xls','w')
f.write(xls)
f.close()

exit("now you can open the output.xlswith excel")
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Converting Refer/BibIX to Excel format

As I promised, I come up with a clean code for converting the Refer/BibIX ( also Zotoro exported collection ) to a clear Excel format.

#! /usr/bin/env python

import sys
import os

def index_2d(myList, v):
	output = ""
	for i, x in enumerate(myList):
		if v in x:
			output += x[3:]
	return output

txt = ""
xls = ""

directory = sys.argv[1]

if len(sys.argv) < 1:
	sys.exit('Usage: ./zotoro.py directory')

if not os.path.exists(directory): 
	exit('Directory not found!.nUsage: ./zotoro.py directory')

files = os.listdir(directory)

for theFile in files:
	with open(directory+'/'+theFile) as infile:
		for line in infile:
			txt += line

TagsMap = [[0,'%0','ReferenceType'], [1,'%A','Author'], [2,'%T','Title'], [3,'%8','Date'], [4,'%B','BookTitle'], 
[5,'%D','Year'], [6,'%N','Issue'], [7,'%P','Pages'], [8,'%U','URL'], [9,'%V','Volume'], [10,'%X','Abstract'], 
[11,'%I','Publisher'], [12,'%J','JournalName'], [13,'%K','Keywords'], [14,'%E','Editor'], [15,'%F','Label'], 
[16,'%C','PlacePublished'], [17,'%S','TertiaryTitle'], [18,'%Y','Translator'], [19,'%Z','Notes'], 
[20,'%6','NumberofVolumes'], [21,'%L','CallNumber'], [22,'%M','AccessionNumber'], [23,'%7','Edition'], 
[24,'%9','TypeofWork'], [25,'%?','SubsidiaryAuthor'], [26,'%@','ISBN|ISSN'], [27,'%!','ShortTitle'], 
[28,'%[','OriginalPublication'], [29,'%]','ReprintEdition'], [30,'%*','ReviewedItem'], [31,'%+','AuthorAddress'], 
[32,'%^','Caption'], [33,'%1','Custom1'], [34,'%2','Custom2'], [35,'%3','Custom3'], [36,'%4','Custom4'], 
[37,'%#','Custom5'], [38,'%$','Custom6'], [39,'%&','Section']]

Records = txt.split('rnrn')

# adding the titles of the columns
for i,tag,title in TagsMap:
	if i<len(TagsMap)-1: xls += TagsMap[i][2] + 't'
	else: xls += TagsMap[i][2] + 'n'

for record in Records:
	Fields = record.split('rn')
	for i,tag,title in TagsMap:
		if i<len(TagsMap)-1: xls += index_2d(Fields,tag) + 't'
		else: xls += index_2d(Fields,tag) + 'n'

f = open(directory+'/output.xls','w')
f.write(xls)
f.close()

exit("now you can open the output.xlswith excel")

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s