Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert csv exported from Google Sheets to RIS format. To use: python csv-to-ris-format.py infile.csv outfile.txt
#!/usr/bin/env python
# To use:
# python csv-to-ris-format.py csvfile.csv risoutput.txt
#
# Assumes you have removed the header row from the csv file,
# columns are in the same order as the labels list,
# and there are no other columns in the csv.
import csv
from sys import argv
inputfile = argv[1]
outputfile = argv[2]
items = []
labels = ["AU", "TI", "VL", "IS", "DA", "SP", "EP", "PB", "T2", "N1", "ER"]
with open(inputfile, 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in reader:
# in order from csv made from Google Sheet
print("reading a row")
# create a list of tuples where the first value is the two-letter label,
# second value is a field in the row from the csv
item = zip(labels, row)
items.append(item)
with open(outputfile, 'w') as risfile:
for citation in items:
print("writing a row")
# citation type is article
risfile.write("TY - JOUR \n")
for field in citation:
line = "{0} - {1}\n".format(field[0], field[1])
risfile.write(line)
# add required end-of-record row
risfile.write("ER - \n")
@dumy55

This comment has been minimized.

Copy link

@dumy55 dumy55 commented Aug 8, 2018

#!/usr/bin/env python

To use:

python csv-to-ris-format.py csvfile.csv risoutput.txt

Assumes you have removed the header row from the csv file,

columns are in the same order as the labels list,

and there are no other columns in the csv.

import csv
from sys import argv

inputfile = argv[1]
outputfile = argv[2]

items = []
labels = ["AU", "TI", "VL", "IS", "DA", "SP", "EP", "PB", "T2", "N1", "ER"]

with open(inputfile, 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='"')

for row in reader:
   	# in order from csv made from Google Sheet
    print("reading a row")
    # create a list of tuples where the first value is the two-letter label,
    # second value is a field in the row from the csv
    item = zip(labels, row)
    items.append(item)

with open(outputfile, 'w') as risfile:
for citation in items:
print("writing a row")
# citation type is article
risfile.write("TY - JOUR \n")
for field in citation:
line = "{0} - {1}\n".format(field[0], field[1])
risfile.write(line)
# add required end-of-record row
risfile.write("ER - \n")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.