新人求解: Python 写入 txt 时数据丢失的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
chaneyccy
V2EX    Python

新人求解: Python 写入 txt 时数据丢失的问题

  •  1
     
  •   chaneyccy 2020-01-14 15:15:10 +08:00 3595 次点击
    这是一个创建于 2102 天前的主题,其中的信息可能已经有所发展或是发生改变。

    主要问题在这:

    for txt in list_: txts = txt.get_text() #在这里 print(txts), 结果还是完整的 download_run(title1, title2, title3, title4, txts) #在这里 print(txts), 数据只剩最后一个段落了

    代码如下: def download(href_urls): for url in href_urls: mod_titles = [] ses = requests.session() html = ses.get(url, headers = header(), verify = False) soup = BeautifulSoup(html.content, 'html.parser') title_list = soup.find(class_ = 'g-ctnBar').find_all('a') title1 = title_list[2].get_text() title2 = title_list[3].get_text() title3 = title_list[4].get_text() title4 = title_list[5].get_text() list_ = soup.find_all('div', class_ = 'detail-mod J_floor')[:-3] for txt in list_: txts = txt.get_text() download_run(title1, title2, title3, title4, txts)

    def download_run(title1, title2, title3, title4, txts): path = 'C:/Users/Desktop/run/%s/%s/%s' %(title1, title2, title3) if not os.path.exists(path): os.makedirs(path) with open('C:/Users/Desktop/run/%s/%s/%s/%s.txt' %(title1, title2, title3, title4), 'w')as f: f.write(txts)

    18 条回复    2020-01-15 12:03:16 +08:00
    chaneyccy
        1
    chaneyccy  
    OP
       2020-01-14 15:23:28 +08:00
    排版有点乱,更新一下

    def download(href_urls):

    for url in href_urls:

    mod_titles = []

    ses = requests.session()

    html = ses.get(url, headers = header(), verify = False)

    soup = BeautifulSoup(html.content, 'html.parser')

    title_list = soup.find(class_ = 'g-ctnBar').find_all('a')

    title1 = title_list[2].get_text()

    title2 = title_list[3].get_text()

    title3 = title_list[4].get_text()

    title4 = title_list[5].get_text()

    list_ = soup.find_all('div', class_ = 'detail-mod J_floor')[:-3]

    for txt in list_:

    txts = txt.get_text()

    download_run(title1, title2, title3, title4, txts)


    def download_run(title1, title2, title3, title4, txts):

    path = 'C:/Users/Desktop/run/%s/%s/%s' %(title1, title2, title3)

    if not os.path.exists(path):

    os.makedirs(path)

    with open('C:/Users/Desktop/run/%s/%s/%s/%s.txt' %(title1, title2, title3, title4), 'w')as f:

    f.write(txts)
    JCZ2MkKb5S8ZX9pq
        2
    JCZ2MkKb5S8ZX9pq  
       2020-01-14 15:24:21 +08:00 via iPhone
    ```
    你的代码
    ```

    这样可以保留模式。回复时无效。详情可查 markdown 格式。
    JCZ2MkKb5S8ZX9pq
        3
    JCZ2MkKb5S8ZX9pq  
       2020-01-14 15:24:47 +08:00 via iPhone
    格式
    chaneyccy
        4
    chaneyccy  
    OP
       2020-01-14 15:28:06 +08:00
    @JCZ2MkKb5S8ZX9pq 好的,平时没有用 markdown 写内容的习惯~ 我去研究下
    cxyfreedom
        5
    cxyfreedom  
       2020-01-14 15:31:05 +08:00
    你遍历循环又每次写入,你循环完成后,txts 本来就只有一部分的数据,写入到文件中当然就只有最后一部分。
    Vegetable
        6
    Vegetable  
       2020-01-14 15:31:27 +08:00
    Vegetable
        7
    Vegetable  
       2020-01-14 15:31:58 +08:00   1
    @cxyfreedom #5 代码我读了,是因为每次都 open(file,'w')写入的原因。
    Wuuuu
        8
    Wuuuu  
       2020-01-14 15:32:56 +08:00
    感觉应该是 txts = txts+"\n"+txt.get_txt()?
    Wuuuu
        9
    Wuuuu  
       2020-01-14 15:35:13 +08:00
    @Vegetable py 靠缩进……这样的不知道到底是
    for txt in list_:

    txts = txt.get_text()

    download_run(title1, title2, title3, title4, txts)

    还是
    for txt in lis_:

    txts = txt.get_text()

    download_run(title1, title2, title3, title4, txts)

    但大概率是第二种写法吧。
    Wuuuu
        10
    Wuuuu  
       2020-01-14 15:35:53 +08:00
    for txt in list_:

    \t txts = txt.get_text()

    \t download_run(title1, title2, title3, title4, txts)

    for txt in list_:

    \t txts = txt.get_text()

    download_run(title1, title2, title3, title4, txts)
    sDG9xz87SqqCC3mN
        11
    sDG9xz87SqqCC3mN  
       2020-01-14 15:37:00 +08:00
    把 w 改成 a
    zhejiangblue
        12
    zhejiangblue  
       2020-01-14 15:37:09 +08:00
    打开模式用 a,w 会重新创建文件的
    chaneyccy
        13
    chaneyccy  
    OP
       2020-01-14 15:40:03 +08:00
    @Vegetable 感谢感谢,之前把从'a'改成'w'了,没考虑到这个原因,调回来正常了
    cxyfreedom
        14
    cxyfreedom  
       2020-01-14 15:40:50 +08:00
    @Vegetable 如果 for 循环下面的话那就是这个问题。追加写入不能用 w。按这个代码逻辑不如一次性写入呢
    vincexu
        15
    vincexu  
       2020-01-14 15:49:38 +08:00
    JCZ2MkKb5S8ZX9pq
        16
    JCZ2MkKb5S8ZX9pq  
       2020-01-14 17:23:51 +08:00 via iPhone
    如果是 aw 的问题 建议拼接 txt 然后 w
    a 万一重复执行会重复写入
    cherbim
        17
    cherbim  
       2020-01-14 17:39:08 +08:00
    你文件写入的是 w,每次都会重写,用 a 追加啊
    shunfa52000
        18
    shunfa52000  
       2020-01-15 12:03:16 +08:00
    上次我程序结尾没有加 f.close,结果循环次数多的时候报错了
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     888 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 21:24 PVG 05:24 LAX 14:24 JFK 17:24
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86