2017-09-07 21 views
0

最大100K文字の文字列を扱い、csvファイルに異なる列に書きたい。以下は分割されたhtml文字列を分割する

(基本的には32KのExcelのセルの限界を克服しようとする)のサンプルコードです:

soup = BeautifulSoup(r.content, 'html5lib') 
html = str(soup.select('div.DocumentText')) 
if len(html) > 32000: 
    #How to handle here and assign to different variable ex: html1, html2 is the question 
    x.writerow([html_1,......, html_5]) 

例が達成しようとして流れ

  • スクラップウェブサイト
  • 廃棄データ文字がより大きい場合32000以上100K未満
  • 廃棄物を異なる変数に分割
  • 各変数をCSVファイルの異なる列に書き込む
+0

件までのみcase_html(文字列)の長さを扱うことができている、あなたがしたいことを意味しています'c.case_html'をサイズ32kのアイテムに分割しますか? – chowsai

+0

html入力の例と取得したいcsv出力を投稿してください。 – JoshRomRock

+0

スプリットは単語境界で行う必要がありますか? –

答えて

0

希望を聞いて満足し、より良い方法があれば、これは...誰にも役立ちます。..制限は98K

def strhandler(case_html, length): 
    string = case_html 
    return (string[0+i:length+i] for i in range(0, len(string), length)) 

case_html = str(soup.find('div', class_='DocumentText').find_all(['p','center','small'])) 
char_count = len(c.case_html) 
split_no = int(char_count/4) 
print('Split this into no.of columns', split_no) 
case_html_1, case_html_2, case_html_3, case_html_4, case_html_5 = list(c.strhandler(case_html,split_no)) 
csv_writer.writerow([case_html_1, case_html_2, case_html_3, case_html_4, case_html_5,]) 
1

これを試してみてください。文字列を32000のサイズに分割し(必要に応じてサイズを変更するだけ)、リストに入れます。

if len(html) > 32000: 
    #How to handle here and assign to different variable ex: html1, html2 is the question 
    output = [html[0+i:32000+i] for i in range(0, len(html), 32000)] 
    x.writerow(output)