0

Im taking info from my school grade site and its coming out as a long string this is the output:

[u'--']
[u'B', u'84']
[u'--']
[u'A-', u'90']
[u'--']
[u'C+', u'79']
[u'--']
[u'A', u'95']
[u'--']
[u'B', u'82']
[u'--']
[u'B', u'81']

Im using this to get the out put

for cell in driver.find_elements_by_css_selector(".grid tr a[href$='fg=S2']"):
    gradesList = cell.text.split('\n')
    print gradesList

i want to assign each of the grades a variable but im not sure how to get them all to separate to each variable

Serial
  • 7,925
  • 13
  • 52
  • 71
  • You can treat each one as a separate variable in that you can access the elements of the list `gradesList[0]` for example. – squiguy May 06 '13 at 05:01
  • if i `print gradesList[0]` it print just the letters for the grades but they are still all together – Serial May 06 '13 at 05:09

1 Answers1

1

It looks like the .split call in your loop returns a different number of elements depending on the HTML tag you're looking at. If there isn't any way to filter out those bad tags, here's a clean way with destructuring assignment, skipping the element if necessary:

for cell in driver.find_elements_by_css_selector(".grid tr a[href$='fg=S2']"):
    gradesList = cell.text.split('\n')
    if len(gradesList) < 2:
        continue

    letter_grade, score = gradesList

    # Use variables...

If you can figure out a way to avoid the elements with only "--" as text (extra CSS class? content filter with your parser library?) we can get a nice list comprehension:

grade_elements = driver.find_elements_by_css_selector(".grid tr a[href$='fg=S2']")
grades = [g.text.split('\n') for g in grade_elements]
Jon Gauthier
  • 25,202
  • 6
  • 63
  • 69
  • See my updated answer. If you have a way of filtering out the elements with only '--', you can get a very simple solution. – Jon Gauthier May 06 '13 at 05:16
  • i got the grades but there like this `[u'B', u'84'] – Serial May 06 '13 at 05:44
  • @Christian - python unicode (http://docs.python.org/2/howto/unicode.html). If you need ascii (http://stackoverflow.com/questions/1207457/convert-unicode-to-string-in-python-containing-extra-symbols) – poof May 06 '13 at 06:34