Python 3 - chiziq/paragraf uzilishlarini qanday o'chirish mumkin

from docx import Document

alphaDic = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','!','?','.','~',',','(',')','$','-',':',';',"'",'/']

while docIndex < len(doc.paragraphs):
    firstSen = doc.paragraphs[docIndex].text
    rep_dic = {ord(k):None for k in alphaDic + [x.upper() for x in alphaDic]}
    translation = (firstSen.translate(rep_dic))
    removeSpaces = " ".join(translation.split())
    removeLineBreaks = removeSpaces.replace('\n','')
    doc.paragraphs[docIndex].text = removeLineBreaks

    docIndex +=1

Hujjatdagi chiziqli uzilishlarni olib tashlashga harakat qilaman, lekin u ishlamayapti. Men hali ham olaman

Hello


There

Dan ko'ra

Hello
There
0
("\ r \ n", '') o'rniga harakat qilib ko'ring.
qo'shib qo'ydi muallif Tianyun Ling, manba

6 javoblar

To'plam namunaviy dastur yordamida keltirilgan. matn.

Ya'ni, sizning muammolar paragraflarda ishlashga harakat qilayotganingizdan kelib chiqadi deb o'ylayman. Biroq paragraflar orasidagi farq yangi satrlarni amalga oshiradigan joydir. Ya'ni, dasturni bo'sh satr bilan almashtirgan bo'lsangiz ham ( "'' ), oxiriga qo'shilgan yangi satr mavjud bo'ladi.

You should either take the approach of the example program, and do your own formatting, or you should make sure that you delete any spurious "empty" paragraphs that might be between the "full" ones you have ("Hello", "", "There") -> ("Hello", "There").

1
qo'shib qo'ydi

Menimcha, siz nima qilishni xohlasangiz, bo'sh so'zlardan qutulasiz. Quyidagi funktsiya sizga yordam berishi mumkin, siz istamagan ma'lum bir xatboshini o'chirib tashlaydi:

  o'chirish parametr (xat):
    p = xat
    p.getparent (). o'chirish (p)
    p._p = p._element = yo'q
 
-ni tanlang      

Kodni qidiring: Scanny *

Kodingizda siz tarjimaning '' ga teng yoki yo'qligini tekshirib ko'rishingiz mumkin va agar u keyin delete_paragraph funksiyasini chaqirsa, sizning kodingiz shunday bo'ladi:

while docIndex < len(doc.paragraphs):
    firstSen = doc.paragraphs[docIndex].text
    rep_dic = {ord(k):None for k in alphaDic + [x.upper() for x in alphaDic]}
    translation = (firstSen.translate(rep_dic))
    if translation != '':
        doc.paragraphs[docIndex].text = translation 
    else:
        delete_paragraph(doc.paragraphs[docIndex])
        docIndex -=1 # go one step back in the loop because of the deleted index

    docIndex +=1

*Reference- feature: Paragraph.delete()

1
qo'shib qo'ydi

Menimcha, siz nima qilishni xohlasangiz, bo'sh so'zlardan qutulasiz. Quyidagi funktsiya sizga yordam berishi mumkin, siz istamagan ma'lum bir xatboshini o'chirib tashlaydi:

  o'chirish parametr (xat):
    p = xat
    p.getparent (). o'chirish (p)
    p._p = p._element = yo'q
 
-ni tanlang      

Kodni qidiring: Scanny *

Kodingizda siz tarjimaning '' ga teng yoki yo'qligini tekshirib ko'rishingiz mumkin va agar u keyin delete_paragraph funksiyasini chaqirsa, sizning kodingiz shunday bo'ladi:

while docIndex < len(doc.paragraphs):
    firstSen = doc.paragraphs[docIndex].text
    rep_dic = {ord(k):None for k in alphaDic + [x.upper() for x in alphaDic]}
    translation = (firstSen.translate(rep_dic))
    if translation != '':
        doc.paragraphs[docIndex].text = translation 
    else:
        delete_paragraph(doc.paragraphs[docIndex])
        docIndex -=1 # go one step back in the loop because of the deleted index

    docIndex +=1

*Reference- feature: Paragraph.delete()

1
qo'shib qo'ydi

Menimcha, siz nima qilishni xohlasangiz, bo'sh so'zlardan qutulasiz. Quyidagi funktsiya sizga yordam berishi mumkin, siz istamagan ma'lum bir xatboshini o'chirib tashlaydi:

  o'chirish parametr (xat):
    p = xat
    p.getparent (). o'chirish (p)
    p._p = p._element = yo'q
 
-ni tanlang      

Kodni qidiring: Scanny *

Kodingizda siz tarjimaning '' ga teng yoki yo'qligini tekshirib ko'rishingiz mumkin va agar u keyin delete_paragraph funksiyasini chaqirsa, sizning kodingiz shunday bo'ladi:

while docIndex < len(doc.paragraphs):
    firstSen = doc.paragraphs[docIndex].text
    rep_dic = {ord(k):None for k in alphaDic + [x.upper() for x in alphaDic]}
    translation = (firstSen.translate(rep_dic))
    if translation != '':
        doc.paragraphs[docIndex].text = translation 
    else:
        delete_paragraph(doc.paragraphs[docIndex])
        docIndex -=1 # go one step back in the loop because of the deleted index

    docIndex +=1

*Reference- feature: Paragraph.delete()

1
qo'shib qo'ydi

Menimcha, siz nima qilishni xohlasangiz, bo'sh so'zlardan qutulasiz. Quyidagi funktsiya sizga yordam berishi mumkin, siz istamagan ma'lum bir xatboshini o'chirib tashlaydi:

  o'chirish parametr (xat):
    p = xat
    p.getparent (). o'chirish (p)
    p._p = p._element = yo'q
 
-ni tanlang      

Kodni qidiring: Scanny *

Kodingizda siz tarjimaning '' ga teng yoki yo'qligini tekshirib ko'rishingiz mumkin va agar u keyin delete_paragraph funksiyasini chaqirsa, sizning kodingiz shunday bo'ladi:

while docIndex < len(doc.paragraphs):
    firstSen = doc.paragraphs[docIndex].text
    rep_dic = {ord(k):None for k in alphaDic + [x.upper() for x in alphaDic]}
    translation = (firstSen.translate(rep_dic))
    if translation != '':
        doc.paragraphs[docIndex].text = translation 
    else:
        delete_paragraph(doc.paragraphs[docIndex])
        docIndex -=1 # go one step back in the loop because of the deleted index

    docIndex +=1

*Reference- feature: Paragraph.delete()

1
qo'shib qo'ydi

Readlines har qanday matnli fayllarni o'qishi mumkin bo'lganligi sababli faylni kerakli qatorlarni qayta yozishingiz va foydalanmoqchi bo'lmagan qatorlarni e'tiborsiz qoldirishingiz mumkin.

"""example"""

file = open("file name", "w")
for line in file.readlines():
    if (line != ''):
        file.write(line)
0
qo'shib qo'ydi
Ushbu kod snippetani savolni hal qilishi mumkin bo'lsa-da, izohni o'z ichiga oladigan , albatta, takomillashtirishga yordam beradi Sizning postingizning sifati. Kelajakda o'quvchilarga bo'lgan savolingizga javob berishni unutmang va ular sizning kod taklifingizni sabablarini bilmasligi mumkin.
qo'shib qo'ydi muallif DimaSan, manba
Tanishish uchun rahmat. men eng yaxshi harakat qilaman.
qo'shib qo'ydi muallif NINO Cikoo, manba
Python
Python
372 ishtirokchilar

Bu guruh python dasturlash tilini muhokama qilish uchun. Iltimos, o'zingizni hurmat qiling va faqat dasturlash bo'yicha yozing. Botlar mavzusini @botlarhaqida guruhida muhokama qling! FAQ: @PyFAQ Offtopic: @python_uz_offtopic

Python offtopic group !
Python offtopic group !
150 ishtirokchilar

@python_uz gruppasining offtop gruppasi. offtop bo'lsa ham reklama mumkin emas ) Boshqa dasturlash tiliga oid gruppalar @languages_programming