问题描述
尝试编写一个生成器函数,该函数一次读取一行文件,并根据定义的分隔符将每个项目作为列表中的单独元素输出。因此,输入:
ID|Name|Major
1234|Jane Heng|History
2334|Nandini Khola|Computer Science
6345|Ben Johnson|Data Science
理想的输出为:
[1234,Jane Heng,History]
[2334,Nandini Khola,Computer Science]
[6345,Ben Johnson,Data Science]
这是我到目前为止的代码:
def file_reader(path,fields,sep,header):
with open(path,"r") as file:
if not os.path.isfile(path):
raise FileNotFoundError(
errno.ENOENT,os.strerror(errno.ENOENT),path)
for line in file:
count = 0 #Initialize line counter
while True:
i = line.find(sep)
count += 1
if i == -1:
break
fieldlist = [x for x in (line.rstrip(sep) for line in file) if x]
# if header is True:
# if len(fieldlist) == fields:
# count = 1 # Start from the second line if there is header
# continue
# else:
# raise ValueError(
# f'{path} has {len(fieldlist)} fields in header but expected {fields} fields!')
if len(fieldlist) != fields:
raise ValueError(f'{path} has {len(fieldlist)} fields on line {count} but expected {fields} fields!')
yield fieldlist
但是要测试:
gen = file_reader('/path/to/file.txt',3,sep='|',header=True)
print(next(gen))
我得到:
['1234|Jane Heng|History\n','2334|Nandini Khola|Computer Science\n','6345|Ben Johnson|Data Science']
如果我尝试类似的东西
for ID,Name,Major in file_reader('/path/to/file.txt',header=True):
print(f"id: {ID} name: {Name} major: {Major}")
我得到的输出是:
cwid: 1234|Jane Heng|History
name: 2334|Nandini Khola|Computer Science
major: 6345|Ben Johnson|Data Science
ValueError: /path/to/file.txt has 0 fields on line 2 but expected 3 fields!
显然,\n
会将所有内容读取为1行,因此出现ValueError异常。
标头代码块当前已被注释掉,但其想法是仅在标头具有预期的字段数时才继续。因此,如果标头中只有2个字段,则将引发ValueError异常。当该块被注释时,我得到:
ValueError: /path/to/file.txt has 0 fields in header but expected 3 fields!
关于如何获得所需输出的任何建议?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)