Last active
December 21, 2022 20:56
-
-
Save lwrubel/e6e8acfa6318771fa4383cabec05f798 to your computer and use it in GitHub Desktop.
Convert csv exported from Google Sheets to RIS format. To use: python csv-to-ris-format.py infile.csv outfile.txt
This file contains hidden or 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
#!/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") |
Hey Laura,
I am trying to use your script to convert a CSV file exported from ASReview. However, my CSV seems having much more columns than you do, like I have many AU for one entry, how do you handle that, thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
could you help me with it