0

I have some data that I've pulled from a website. This is the code I used to grab it (my actual code is much longer but I think this about sums it up).

lid_restrict_save = []
for t in range(10000,10020): 

    address = 'http://www.tspc.oregon.gov/lookup_application/' + lines2[t]

    page = requests.get(address)

    tree = html.fromstring(page.text)

    #District Restriction
    dist_restrict = tree.xpath('//tr[11]//text()')
    if u"District Restriction" in dist_restrict:
        lid_restrict_save.append(id2)

I'm trying to export this list:

print lid_restrict_save
[['5656966VP65', '5656966RR68', '56569659965', '56569658964']]

to a text file.

f = open('dis_restrict_no_uniqDOB2.txt', 'r+')

for j in range(0,len(lid_restrict_save)):
    s = ( (unicode(lid_restrict_save[j]).encode('utf-8') + ' \n' ))
    f.write(s) 

f.close()

I want the text to come out looking like this:

5656966VP65
5656966RR68
56569659965
56569658964

This code worked but only when I started the range from 0.

f = open('dis_restrict.txt', 'r+')

for j in range(0,len(ldob_restrict)):
    f.write( ldob_restrict[j].encode("utf-8") + ' \n' )

f.close()

When I've tried changing the code I keep getting this error:

"AttributeError: 'list' object has no attribute 'encode'."

I've tried the suggestions from here, here, and here but to no avail.

If anyone has any hints it would be greatly appreciated.

Community
  • 1
  • 1
otteheng
  • 594
  • 1
  • 9
  • 27
  • `lid_restrict_save` is a nested list. Use `lid_restrict_save = lid_restrict_save[0]` – Farhan.K Mar 31 '16 at 14:11
  • @Farhan.K I'm getting `IndexError: list index out of range`. Should that be nested within the loop or below `lid_restrict_save = []`? – otteheng Mar 31 '16 at 14:24

1 Answers1

0

lid_restrict_save is a nested list so you can't encode the first element because it is not a string.

You could write to the txt file using this:

lid_restrict_save = [['5656966VP65', '5656966RR68', '56569659965', '56569658964']]

lid_restrict_save = lid_restrict_save[0] # remove the outer list

with open('dis_restrict.txt', 'r+') as f:
    for i in lid_restrict_save:
        f.write(str(i) + '\n')
Farhan.K
  • 3,425
  • 2
  • 15
  • 26
  • This is slightly unrelated. I'm exporting a series of elements (name, employer, etc.) to a csv and they to are a nested list. I used `lname = lname[0]` and the outer brackets disappeared but then only one item printed out. Do you know how to get the brackets to disappear but also print through `range` 10000-10020? – otteheng Mar 31 '16 at 14:45
  • You would need to put `lname = lname[0]` _before_ the loop. My guess is that you are putting it inside the loop? – Farhan.K Mar 31 '16 at 14:47
  • I was putting it after the loop. When I put it before the loop I get `IndexError: list index out of range`. – otteheng Mar 31 '16 at 14:52
  • That sounds like another issue. I'm sorry I can't really help you without seeing your code. – Farhan.K Mar 31 '16 at 15:26
  • It's not a big issue. I was just curious if a similar coding would work. Thanks for your help though. – otteheng Mar 31 '16 at 15:50