Boshqa faylda mavjud bo'lgan fayllarni bitta python yordamida o'chirish

Satrlarni ishlatib, faylni taqdim etadigan satrlarni olib tashlash bilan biriktirildim, boshqa faylni python-da taqdim qilaman. Iplarni string moslashtirish uchun ikkitadan foydalanmoqchiman, lekin loopning ikkinchi yo'nalishida ikki nusxadagi chiziqlarni beradi. Buning boshqa yo'llari bormi? Rahmat

with open('results.csv','r+') as source:
    lines = source.readlines()

f = open('results_comments.csv','r')
line = f.readlines()
for l in line:
    for L in lines:
        if L!=l:
            source.write(L)
f.close()
1
Agar siz uni ishlagan misolni kiritish uchun tahrir qilsangiz, savolingiz aniqroq bo'ladi.
qo'shib qo'ydi muallif Martin Evans, manba
Fayllarning nomidan csv fayllari bilan ishlash kabi ko'rinadi. Men haqmi?
qo'shib qo'ydi muallif styvane, manba
Fayllarning nomidan csv fayllari bilan ishlash kabi ko'rinadi. Men haqmi?
qo'shib qo'ydi muallif styvane, manba
Yo'q, faqat nomlash uchun. uning rejasi matn.
qo'shib qo'ydi muallif Balaji, manba
Yo'q, faqat nomlash uchun. uning rejasi matn.
qo'shib qo'ydi muallif Balaji, manba

6 javoblar

O'zgaruvchining ismlaringiz shunchaki aralashdi, shuning uchun men juftlikni o'zgartirdim. Natijalarni yozish uchun siz yangi faylni ochishingiz kerak deb o'ylayman (hech bo'lmaganda bu siz nima qilmoqchi ekaningizni ravshan qiladi). Bir xil nomni xohlasangiz, faylni keyinroq nomini o'zgartirishingiz mumkin.

with open('results.csv','r+') as source:
    filter_lines = source.readlines()

with open('results_comments.csv','r') as f:
    lines = f.readlines()

with open('target.csv', 'w') as target:
    for line in lines:
        if line not in filter_lines:
            target.write(line)
1
qo'shib qo'ydi

O'zgaruvchining ismlaringiz shunchaki aralashdi, shuning uchun men juftlikni o'zgartirdim. Natijalarni yozish uchun siz yangi faylni ochishingiz kerak deb o'ylayman (hech bo'lmaganda bu siz nima qilmoqchi ekaningizni ravshan qiladi). Bir xil nomni xohlasangiz, faylni keyinroq nomini o'zgartirishingiz mumkin.

with open('results.csv','r+') as source:
    filter_lines = source.readlines()

with open('results_comments.csv','r') as f:
    lines = f.readlines()

with open('target.csv', 'w') as target:
    for line in lines:
        if line not in filter_lines:
            target.write(line)
1
qo'shib qo'ydi

Siz quyidagilarni sinashingiz mumkin:

with open('results.csv','r') as source:
    lines_src = source.readlines()
with open('results_comments.csv','r') as f:
    lines_f = f.readlines()
destination = open("destination.csv","w")
for data in lines_src:
    if data not in lines_f:
        destination.write(data)
destination.close()
1
qo'shib qo'ydi
marvarid kabi ishlaydi, rahmat
qo'shib qo'ydi muallif Balaji, manba

Siz quyidagilarni sinashingiz mumkin:

with open('results.csv','r') as source:
    lines_src = source.readlines()
with open('results_comments.csv','r') as f:
    lines_f = f.readlines()
destination = open("destination.csv","w")
for data in lines_src:
    if data not in lines_f:
        destination.write(data)
destination.close()
1
qo'shib qo'ydi
marvarid kabi ishlaydi, rahmat
qo'shib qo'ydi muallif Balaji, manba
with open('results.csv','r+') as source:
     lines = source.readlines()

f = open('results_comments.csv','r')
line = f.readlines()
line = filter(lambda x : x not in lines, line)
f.write('\n'.join(line))
f.close()
0
qo'shib qo'ydi
with open('results.csv','r+') as source:
     lines = source.readlines()

f = open('results_comments.csv','r')
line = f.readlines()
line = filter(lambda x : x not in lines, line)
f.write('\n'.join(line))
f.close()
0
qo'shib qo'ydi
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