我试图通过将分隔符“管道”的文件转换为csv文件(3个文件)来了解生成器的工作原理。
当我运行以下代码时,它仅转换和写入1个csv文件(c.csv)。
我期望它将转换并写入所有三个文件(a.csv,b.csv和c.csv)
专家,您能帮我理解为什么它不能转换3个文件吗?
每个文件中的数据(3个文件):
country|city
代码:
from typing import Iterator, Iterable
ret={
"data": [
{
"src": "/Users/home/Desktop/python/a.txt"
},
{
"src": "/Users/home/Desktop/python/b.txt"
},
{
"src": "/Users/home/Desktop/python/c.txt"
}]}
def extract(files:Iterable[dict])-> Iterator[dict]:
for file in files:
ret = file.copy()
with open(file['src']) as f:
ret['data']=f.read()
yield ret
def transform(files: Iterable[dict]) -> Iterator[dict]:
for file in files:
df = file_to_df(file["data"])
ret = file.copy()
ret['data']=df
yield ret
def load(files: Iterable[dict]) -> Iterator[dict]:
for file in files:
f = open(file['src'].replace(".txt",".csv"),'w')
f.write(file['data'])
ret = dict(file)
ret['dest']=file['src'].replace(".txt",".csv")
yield ret
# Convert pipe to comma
def file_to_df (file:str, boilerplate_stripped=False):
df = file.replace("|",",")
return df
# Transform pipe delimited files to csv
raw_files = extract(ret['data'])
transform_files = transform(raw_files)
load_files = load(transform_files)
keep_keys = ['src', 'dest']
ret = {
"data": [dict({k: v for k, v in x.items() if k in keep_keys}) for x in load_files]
}
print (ret)
结果:
{'data': [{'src': '/Users/home/Desktop/python/c.txt', 'dest': '/Users/home/Desktop/python/c.csv'}]}
预期结果:
{'data': [{'src': '/Users/home/Desktop/python/a.txt', 'dest': '/Users/home/Desktop/python/a.csv'},
{'src': '/Users/home/Desktop/python/b.txt', 'dest': '/Users/home/Desktop/python/b.csv'},
{'src': '/Users/home/Desktop/python/c.txt', 'dest': '/Users/home/Desktop/python/c.csv'}]}