list comprehension and item with chinese character
<p>I have the following code which will grab some data with Chinese character from a website.</p>

<pre><code>import csv
import requests
from bs4 import BeautifulSoup

url = "http://www.hkcpast.net/cpast_homepage/xyzbforms/BetMatchDetails.asp?tBetDate=2016/9/11"

r = requests.get(url)
soup = BeautifulSoup(r.content, "html.parser")

for a in soup.find_all('html'):
    a.decompose()

list = []
for row in soup.find_all('tr'):
    cols = row.find_all('td')
    for col in cols:
        if len(col) &gt; 0:
            list.append(col.text.encode('utf-8').strip())
</code></pre>

<p>For now the result is like this:</p>

<pre><code>[1, x, y, z, 2, x, y, z, 3, x, y, z]
</code></pre>

<p>My question is I want to create some sublists from the list, which the is separated by a number (1, 2, 3, 4 ,5 . .. . .)</p>

<p>so that the result will be like this:</p>

<pre><code>[1, x, y, z]
[2, x, y, z]
[3, x, y, z]
</code></pre>

<p>the ultimate goal of this is to write each sublist as a row in a csv file. Does it make sense to first separate the list into each entry and then write into a csv file? </p>

<p>The literal translation of your code would look like:</p>

<pre><code>list = []
for row in soup.find_all('tr'):
    cols = row.find_all('td')
    for col in cols:
        if len(col) = 0:
            continue  # Save some indentation
        txt = col.text.encode('utf-8').strip()
        try:
           _ = int(txt)
           # txt is an int.  Append new sub-list
           list.append( [txt] )
        except ValueError:
           # txt is not an int, append it to the end of previous sub-list
           list[-1].append(txt)
</code></pre>

<p>(Note that this will fail horribly if the first entry is not an int!)</p>

<p>However, I suspect you actually want to create a new sub-list for each row in the table.</p>

python list csv