Skip to content

Instantly share code, notes, and snippets.

@lwrubel
Last active December 21, 2022 20:56
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save lwrubel/e6e8acfa6318771fa4383cabec05f798 to your computer and use it in GitHub Desktop.
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
#!/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
Copy link

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")

@DrAhmedTorad
Copy link

it is very good but i usually have this error
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 7942: character maps to

@DrAhmedTorad
Copy link

could you help me with it

@sarkrui
Copy link

sarkrui commented Oct 28, 2022

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