Python o'qish va yozish .txt faylga

Babynames2014.txt faylidan nomlarni o'qib chiqishim kerak, keyin ikkita yangi fayl yaratib, o'g'il va qizlarning ismlarini ajratib olishlari kerak. Olingan fayllar boynames2014.txt va girlnames.txt deb nomlanishi kerak. Babynames2014.txt fayllari shunday ko'rinadi:

1 Nuh Emma

2 Liam Olivia

3 Mason Sofiya

4 Yoqub Isabella

va 100 o'g'il va qizlarning ismlariga etgunga qadar davom etadi. Men hozirgacha yozgan kod yangi matnli fayllarni yaratadi, lekin boynamesda2014 hech narsa yo'q. Qizlar2014 faqat Nuh ismini o'z ichiga oladi: 1Noah.

readline() va line.split() dan foydalanish kerak deb o'ylayman qaerda va qanday qilib ularni to'g'ri ishlatish mumkinligiga ishonchim komil emas. Babynames2014.txt fayl topilmasa istisno qilish uchun Ushalar/tashqari blokidan foydalanish kerak.

    infile = open("babynames2014.txt", "r")    
    outfile = open("boynames2014.txt", "w")
    outfile = open("girlnames2014.txt", "w")


    line = infile.readline()
    datafield = line.split()
    boyname2014 = datafield[0]
    girlname2014 = datafield[1]
    outfile.write(boyname2014)
    outfile.write(girlname2014)


    infile.close()
    outfile.close()

Men 2-3 oy davomida faqat Pythonni o'rganib chiqdim va ko'proq o'rganishimga yordam beradigan har qanday maslahatlarni juda qadrlayman!

1
siz kutgan narsalarni ololmaysiz va biz yordam bera oladigan bir nechta sabablar bor! babynames2014.txt ning misolini bering. EDIT: Men sizning namunangiz borligini sezdim.
qo'shib qo'ydi muallif orsonady, manba

7 javoblar

Men boynames2014.txt va girlnames2014.txt uchun outfile , ya'ni mantiqan to'g'ri emasligini aniqladim.

Siz shunday qilishingiz kerak.

infile = open("babynames2014.txt", "r")    
outfile_boys = open("boynames2014.txt", "w")
outfile_girls = open("girlnames2014.txt", "w")

So'ngra, kerakli ma'lumotlarni quyidagi qatorga qarab yangi liniya bilan taqsimlash kerak.

lines = infile.read().split("\n")

So'ngra quyida ko'rsatilgan satrlarni yineleyin va bo'shliq bilan bo'linadi (standart).

for line in lines:
    datafield = line.split()
    boyname2014 = datafield[1]
    girlname2014 = datafield[2]
    outfile_boys.write(boyname2014 + '\n')
    outfile_girls.write(girlname2014 + '\n')

Ma'lumotlar maydonchasi uchun 1 va 2 indeksni tanladim, chunki faylingiz quyidagicha ma'lumotlarni o'z ichiga oladi:

1 boy_name girl_name

Splitting by space delivers boy_name to 1st index and girl_name to 2nd index

Keyin fayllarni odatdagidek yoping.

infile.close()
outfile_boys.close()
outfile_girls.close()

Umid qilamanki yordam beradi!

1
qo'shib qo'ydi
Andrew N.ga yordam berishdan xursandman
qo'shib qo'ydi muallif abhinav, manba
Katta rahmat! Men hamma narsani tushuntirganingiz uchun chindan ham minnatdorman, bu menga yanada yaxshiroq tushunishga yordam beradi!
qo'shib qo'ydi muallif user7577503, manba

Regex echimini ko'rib chiqmoqchimisiz?

with open("babynames2014.txt", "r") as f1,open("boynames2014.txt", "w") as boys,open("girlnames2014.txt","w") as girls:
    # Note this will not work for name which has speacial charecters like `-,$,etc`
    boy_regex = re.compile(r"^\d\s?([a-zA-z0-9]+)\s[a-zA-z0-9]+$",re.MULTILINE)
    girl_regex = re.compile(r"^\d\s?[a-zA-z0-9]+\s([a-zA-z0-9]+)$",re.MULTILINE)
    boys.write('\n'.join(boy_regex.findall(f1.read())))
    girls.write('\n'.join(girl_regex.findall(f1.read())))
0
qo'shib qo'ydi

Chiqdi fayllari uchun alohida ko'rsatgichlar kerak.

`
infile = open("babynames2014.txt", "r")
outfileboy = open("boynames2014.txt", "w")
outfilegirl = open("girlnames2014.txt", "w")
for line in infile.readlines():
    names = line.split(" ")
    outfileboy.write(str(names[1]+"\n")
    outfilegirl.write(str(names[2]+"\n")

outfileboy.close()
outfilegirl.close()

`
0
qo'shib qo'ydi

readline() only reads a single line (as the name might suggest) so only the first line get read (1 Noah Emma )

Barcha satrlarni o'qish va ularni ajratish va ularni faylga yozish uchun harakat qilib ko'ring:

# use two different names for the files
# you had one name `outfile` which was being 
# overwritten so tht why boy file was empty

infile = open("babynames2014.txt", "r") 
boyfile = open("boynames2014.txt", "w") 
girlfile = open("girlnames2014.txt", "w")

with open('babynames2014', 'r') as f:
    for l in f.readlines(): # notice readlines instead of readline
        _, boy, girl = l.split() # assumes the separator is a space
        print(boy, file=boyfile)
        print(girl, file=girlfile)

# don't forget to close your file desciptors
boyfile.close()
girlfile.close()
0
qo'shib qo'ydi

Siz chiqdi fayllarini bir xil o'zgarmaydigan nomini ko'rsatdingiz. outfile .

    infile = open("babynames2014.txt", "r")    
    outfileb = open("boynames2014.txt", "w")
    outfileg = open("girlnames2014.txt", "w")


    line = infile.readline()
    datafield = line.split()
    boyname2014 = datafield[0]
    girlname2014 = datafield[1]
    outfileb.write(boyname2014)
    outfileg.write(girlname2014)


    infile.close()
    outfileb.close()
    outfileg.close()

va barcha nomlarni olish uchun kirish faylidan pastadir kerak. Nomlarni raqamdan o'chirish uchun '' belgisini ishlatishingiz mumkin (i.isdigit ()] ga tegishli bo'lmagan holda [i i iisis() i] uchun .

    infile = open("babynames2014.txt", "r")    
    outfileb = open("boynames2014.txt", "w")
    outfileg = open("girlnames2014.txt", "w")


    tmp = infile.readline()
    line=''.join([i for i in tmp if not i.isdigit()])
    datafield = line.split()
    boyname2014 = datafield[0]
    girlname2014 = datafield[1]
    outfileb.write(boyname2014)
    outfileg.write(girlname2014)


    infile.close()
    outfileb.close()
    outfileg.close()
0
qo'shib qo'ydi
Afsuski, men buni tuzatdim.
qo'shib qo'ydi muallif Neeraj, manba
har ikkala fayl uchun outfile dan foydalanayotganingiz uchun kodingiz hali ham noto'g'ri!
qo'shib qo'ydi muallif abhinav, manba

Mana,

#! /usr/bin/python
import sys

boy_file = str(sys.argv[1])
girl_file = str(sys.argv[2])

all_records = [line.strip() for line in open('babynames2014', 'r')]

f1 = open(boy_file, "w")
f2 = open(girl_file, "w")

for record in all_records:
        split_record = record.split(' ')
        boy_name = split_record[1]
        girl_name = split_record[2]

        f1.write(boy_name+"\n")
        f2.write(girl_name+"\n")


f1.close()
f2.close()
0
qo'shib qo'ydi
    outfile1 = open("boynames2014.txt", "w")
    outfile2 = open("girlnames2014.txt", "w")
    with open('babynames2014.txt') as infile:
        for line in infile:
            datafield = line.split()
            boyname2014 = datafield[0]
            girlname2014 = datafield[1]
            outfile1.write(boyname2014)
            outfile2.write(girlname2014)
    outfile1.close()
    outfile2.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