0

I'm trying to use csv.reader to create a list of items from a string, but I'm having trouble. For instance, I have the following string:

bibinfo = "wooldridge1999asymptotic, author = \"Wooldridge, Jeffrey M.\", title = \"Asymptotic Properties of Weighted M-Estimators for Variable Probability Samples\", journal = \"Econometrica\", volume = \"\", year = 1999"

And I run the following code:

import csv
from io import StringIO
bibitems = [bibitem for bibitem in csv.reader(StringIO(bibinfo), skipinitialspace = True)][0]

But instead of having a list in which commas within a pair of double-quotes are not considered as separators, I obtain the following (unwanted) result:

['wooldridge1999asymptotic', 'author = "Wooldridge', 'Jeffrey M."', 'title = "Asymptotic Properties of Weighted M-Estimators for Variable Probability Samples"', 'journal = "Econometrica"', 'volume = ""', 'year = 1999']

In other words, it separates some items (like author's surname from first name) when it should not. I followed the tips in this other link, but it seems that I'm missing something else too.

Community
  • 1
  • 1
DS81
  • 1
  • 1

1 Answers1

0

It works if the " is at beginning of the item:

"author = Wooldridge, Jeffrey M."

With the changed text:

>>> s = """wooldridge1999asymptotic, "author = Wooldridge, Jeffrey M.", title = "Asymptotic Properties of Weighted M-Estimators for Variable Probability Samples", journal = "Econometrica", volume = "", year = 1999"""
>>> list(csv.reader(s.splitlines(), skipinitialspace=True))
[['wooldridge1999asymptotic',
  'author = Wooldridge, Jeffrey M.',
  'title = "Asymptotic Properties of Weighted M-Estimators for Variable Probability Samples"',
  'journal = "Econometrica"',
  'volume = ""',
  'year = 1999']]
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • Hi, Mike. Thanks for the reply. The problem is that quotes are supposed to remain around the authors name. (This is a BibTeX data. I obtain it from a regular expression in a website that converts references to BibTeX.) So I need a solution that works with the data the way it is. Thanks anyway. – DS81 Dec 21 '15 at 20:25