我需要从babynames2014.txt文件中读取名称,然后创建两个新文件,分隔男孩和女孩的名字。生成的文件应称为boynames2014.txt和girlnames.txt。 babynames2014.txt文件如下所示:
1 Noah Emma
2 Liam Olivia3 Mason Sophia
4雅各布伊莎贝拉
并继续直到它达到100个男孩和女孩的名字。 我到目前为止编写的代码创建了两个新的文本文件,但是boynames2014没有包含任何内容,而girlnames2014只包含名称Noah,其数字为1之前:1Noah。
我认为我需要使用readline()
和line.split()
在某个地方,我只是不确定在哪里以及如何正确使用它们。如果找不到babynames2014.txt文件,我还需要使用try/except
块来处理异常。
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()
我只学习了2-3个月的Python,非常感谢任何建议,以帮助我了解更多信息!
答案 0 :(得分:1)
我注意到一件事在逻辑上不正确,即boynames2014.txt和girlnames2014.txt的outfile
你应该这样做。
infile = open("babynames2014.txt", "r")
outfile_boys = open("boynames2014.txt", "w")
outfile_girls = open("girlnames2014.txt", "w")
然后,您必须阅读infile并按新行分割所需数据,如下所示。
lines = infile.read().split("\n")
然后迭代如下所示的行并按空格分割(默认)。
for line in lines:
datafield = line.split()
boyname2014 = datafield[1]
girlname2014 = datafield[2]
outfile_boys.write(boyname2014 + '\n')
outfile_girls.write(girlname2014 + '\n')
我为数据字段选择了1和2索引,因为您的文件包含以下数据:
1 boy_name girl_name
按空格分割将 boy_name传递给第一个索引,将 girl_name传递给第二个索引
然后像往常一样关闭文件。
infile.close()
outfile_boys.close()
outfile_girls.close()
希望它有所帮助!
答案 1 :(得分:0)
您需要有输出文件的单独指针。
`
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()
`
答案 2 :(得分:0)
您已为输出文件指定了相同的变量名称。 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()
并且您需要遍历输入文件以获取所有名称。
您可以使用''.join([i for i in s if not i.isdigit()])
从名称中删除号码。
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()
答案 3 :(得分:0)
SPARK_HOME
答案 4 :(得分:0)
readline()
只读取一行(顾名思义)
所以只有第一行才能阅读(1 Noah Emma)
要读取所有行并将它们拆分并将它们写入文件,请尝试:
# 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()
答案 5 :(得分:0)
你走了,
#! /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()
答案 6 :(得分:0)
想要考虑使用正则表达式解决方案吗?
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())))