如何取消格式化文件名?(解析或格式相反)
How to unformat a filename?(Parsing or opposite of format)
我想return这个函数转换后的文件的文件名。
from more_itertools import chunked
def file_conversion(input_file, output_file_pattern, chunksize):
with open(input_file) as fin:
reader = csv.DictReader(fin, delimiter=',')
for i, chunk in enumerate(chunked(reader, chunksize)):
with open(output_file_pattern.format(i), 'w', newline='') as fout:
writer = csv.DictWriter(fout, reader.fieldnames, delimiter='|')
writer.writeheader()
writer.writerows(chunk)
print("Successfully converted into", output_file)
script_dir = os.path.dirname(os.path.abspath(__file__))
dest_dir = os.path.join(script_dir, 'temp')
try:
os.makedirs(dest_dir)
except OSError:
pass
path = os.path.join(dest_dir,'out{:03}.csv' )
#this creates file as out000.csv and more out001.csv or out002.csv depending upon the number of lines
file_conversion('in.csv', path, 7000)
base_name = os.path.basename(path)
print(base_name) #returns out{03}.csv
这会打印出{03}.csv。但是我想要像 out000.csv 这样的文件名的转换结果。 return 有什么办法吗?如果文件比较多,如何return文件名?
好的,你需要在你的函数中创建一个集合并用所有使用的文件名填充它:
def file_conversion(input_file, output_file_pattern, chunksize):
output_filenames = []
with open(input_file) as fin:
reader = csv.DictReader(fin, delimiter=',')
for i, chunk in enumerate(chunked(reader, chunksize)):
output_filename = output_file_pattern.format(i)
with open(output_filename, 'w', newline='') as fout:
output_filenames.append(output_filename)
writer = csv.DictWriter(fout, reader.fieldnames, delimiter='|')
writer.writeheader()
writer.writerows(chunk)
print("Successfully converted into", output_file)
return output_filenames
你可以这样称呼它:
paths = file_conversion('in.csv', path, 7000)
base_names = [os.path.basename(path) for path in paths]
print(base_names)
我想return这个函数转换后的文件的文件名。
from more_itertools import chunked
def file_conversion(input_file, output_file_pattern, chunksize):
with open(input_file) as fin:
reader = csv.DictReader(fin, delimiter=',')
for i, chunk in enumerate(chunked(reader, chunksize)):
with open(output_file_pattern.format(i), 'w', newline='') as fout:
writer = csv.DictWriter(fout, reader.fieldnames, delimiter='|')
writer.writeheader()
writer.writerows(chunk)
print("Successfully converted into", output_file)
script_dir = os.path.dirname(os.path.abspath(__file__))
dest_dir = os.path.join(script_dir, 'temp')
try:
os.makedirs(dest_dir)
except OSError:
pass
path = os.path.join(dest_dir,'out{:03}.csv' )
#this creates file as out000.csv and more out001.csv or out002.csv depending upon the number of lines
file_conversion('in.csv', path, 7000)
base_name = os.path.basename(path)
print(base_name) #returns out{03}.csv
这会打印出{03}.csv。但是我想要像 out000.csv 这样的文件名的转换结果。 return 有什么办法吗?如果文件比较多,如何return文件名?
好的,你需要在你的函数中创建一个集合并用所有使用的文件名填充它:
def file_conversion(input_file, output_file_pattern, chunksize):
output_filenames = []
with open(input_file) as fin:
reader = csv.DictReader(fin, delimiter=',')
for i, chunk in enumerate(chunked(reader, chunksize)):
output_filename = output_file_pattern.format(i)
with open(output_filename, 'w', newline='') as fout:
output_filenames.append(output_filename)
writer = csv.DictWriter(fout, reader.fieldnames, delimiter='|')
writer.writeheader()
writer.writerows(chunk)
print("Successfully converted into", output_file)
return output_filenames
你可以这样称呼它:
paths = file_conversion('in.csv', path, 7000)
base_names = [os.path.basename(path) for path in paths]
print(base_names)