Python:解析文件名,拆分,剥离多个字符
Python: Parse file names, split, and strip multiple characters
我有一个包含图像 (.jpg) 的文件夹,我需要将文件名提取为 CSV,使用 '_'
将它们拆分为多列(使用 headers),然后删除多个字符。
我已经使用以下方法部分完成了这个:
import os, csv
with open('filepath.csv', 'w') as f:
writer = csv.writer(f)
for path, dirs, files in os.walk('dirpath'):
for item in files:
writer.writerow([item])
with open('filepath.csv', 'w') as inf:
with open ('outfile.csv', 'w') as outf:
for line in inf:
outf.write(','.join(line.split('_')))
示例文件名:
firstname_lastname_uniqueid_date_latUKN_longUKN_club.jpg
我上面的代码的结果 returns firstname
、lastname
、uniqueid
、date
、latUKN
、longUKN
和 club.jpg
。
这是我正在寻找的架构,但我还想从 latUKN
和 longUKN
中解析出 'lat'
和 'long'
,如以及删除字符串末尾的 .jpg
。我需要删除字符串 'lat'
和 'long'
因为有包含 latitude/longitude 的文件名,但是 'lat'
和 'long'
是在解析中带来的(例如lat12.34, long54.67
)
如何 remove/strip 删除这些其他字符,并添加 headers?如果没有纬度或经度,我如何将这部分留空而不是填充字符串 'latUKN'
、'longUKN'
。是否可以 运行 遍历整个目录并输出单个 csv?
示例数据
John_Doe_2259153_20171102_latUKN_longUKN_club1.jpg
John_Doe_2259153_20171031_lat123.00_long456.00_club1.jpg
Jane_Doe_5964264_20171101_latUKN_longUKN_club2.jpg
Jane_Doe_5964264_20171029_lat789.00_long012.00_club2.jpg
Joe_Smith_1234564_20171001_lat345.00_long678.00_club3.jpg
当前代码的数据外观:
John|Doe|2259153|20171102|latUKN|longUKN|club1.jpg
John|Doe|2259153|20171031|lat123.00|long456.00|club1.jpg
Jane|Doe|5964264|20171101|latUKN|longUKN|club2.jpg
Jane|Doe|5964264|20171029|lat789.00|long012.00|club2.jpg
Joe|Smith|1234564|20171001|lat345.00|long678.00|club3.jpg
我希望数据如何显示:
John|Doe|2259153|20171102|UKN|UKN|club1
John|Doe|2259153|20171031|123.00|456.00|club1
Jane|Doe|5964264|20171101|UKN|UKN|club2
Jane|Doe|5964264|20171029|789.00|l012.00|club2
Joe|Smith|1234564|20171001|345.00|678.00|club3
如果 a 是给定的字符串,您可以使用类似的东西吗?
a="ukn_abcd.jpg"
for i in "ukn",".jpg":
a=a.replace(i,"")
你可以创建一个这样的函数并将字符串传递给它以删除这些不需要的字符
import csv
l=[]
with open("sf.csv") as csvfile:
reader = csv.reader(csvfile)
for row in reader:
l.append(row)
#print(l)
new_l=str(l).replace("lat","").replace(".jpg","").replace("long","")
#print(new_l)
with open("output_sf", "w") as csvfile:
for data in str(new_l).split(","):
#print(str(data))
csvfile.write(str(data))
因此,当我按照您在上面的示例中给出的方式读取您的输入文件时,这就是我得到的输出。现在您可以尝试将其写入您的 txt/csv 文件。
[['John|Doe|2259153|20171102|UKN|UKN|club1'
'John|Doe|2259153|20171031|123.00|456.00|club1'
'Jane|Doe|5964264|20171101|UKN|UKN|club2'
'Jane|Doe|5964264|20171029|789.00|012.00|club2'
'Joe|Smith|1234564|20171001|345.00|678.00|club3']]
由于两个答案都围绕着使用 find/replace,并没有完全解决问题,我使用以下方法来完成任务:
import csv
infile = open('path', 'r')
outfile = open('path', 'r')
findlist = ['lat', 'long', '.jpg.']
replacelist = ["", "", ""]
rep = dict(zip(findlist, replacelist))
s = infile.read()
for item, replacement in zip(findlist, replacelist):
s = s.replace(item, replacement)
outfile.write(s)
我有一个包含图像 (.jpg) 的文件夹,我需要将文件名提取为 CSV,使用 '_'
将它们拆分为多列(使用 headers),然后删除多个字符。
我已经使用以下方法部分完成了这个:
import os, csv
with open('filepath.csv', 'w') as f:
writer = csv.writer(f)
for path, dirs, files in os.walk('dirpath'):
for item in files:
writer.writerow([item])
with open('filepath.csv', 'w') as inf:
with open ('outfile.csv', 'w') as outf:
for line in inf:
outf.write(','.join(line.split('_')))
示例文件名:
firstname_lastname_uniqueid_date_latUKN_longUKN_club.jpg
我上面的代码的结果 returns firstname
、lastname
、uniqueid
、date
、latUKN
、longUKN
和 club.jpg
。
这是我正在寻找的架构,但我还想从 latUKN
和 longUKN
中解析出 'lat'
和 'long'
,如以及删除字符串末尾的 .jpg
。我需要删除字符串 'lat'
和 'long'
因为有包含 latitude/longitude 的文件名,但是 'lat'
和 'long'
是在解析中带来的(例如lat12.34, long54.67
)
如何 remove/strip 删除这些其他字符,并添加 headers?如果没有纬度或经度,我如何将这部分留空而不是填充字符串 'latUKN'
、'longUKN'
。是否可以 运行 遍历整个目录并输出单个 csv?
示例数据
John_Doe_2259153_20171102_latUKN_longUKN_club1.jpg
John_Doe_2259153_20171031_lat123.00_long456.00_club1.jpg
Jane_Doe_5964264_20171101_latUKN_longUKN_club2.jpg
Jane_Doe_5964264_20171029_lat789.00_long012.00_club2.jpg
Joe_Smith_1234564_20171001_lat345.00_long678.00_club3.jpg
当前代码的数据外观:
John|Doe|2259153|20171102|latUKN|longUKN|club1.jpg
John|Doe|2259153|20171031|lat123.00|long456.00|club1.jpg
Jane|Doe|5964264|20171101|latUKN|longUKN|club2.jpg
Jane|Doe|5964264|20171029|lat789.00|long012.00|club2.jpg
Joe|Smith|1234564|20171001|lat345.00|long678.00|club3.jpg
我希望数据如何显示:
John|Doe|2259153|20171102|UKN|UKN|club1
John|Doe|2259153|20171031|123.00|456.00|club1
Jane|Doe|5964264|20171101|UKN|UKN|club2
Jane|Doe|5964264|20171029|789.00|l012.00|club2
Joe|Smith|1234564|20171001|345.00|678.00|club3
如果 a 是给定的字符串,您可以使用类似的东西吗?
a="ukn_abcd.jpg"
for i in "ukn",".jpg":
a=a.replace(i,"")
你可以创建一个这样的函数并将字符串传递给它以删除这些不需要的字符
import csv
l=[]
with open("sf.csv") as csvfile:
reader = csv.reader(csvfile)
for row in reader:
l.append(row)
#print(l)
new_l=str(l).replace("lat","").replace(".jpg","").replace("long","")
#print(new_l)
with open("output_sf", "w") as csvfile:
for data in str(new_l).split(","):
#print(str(data))
csvfile.write(str(data))
因此,当我按照您在上面的示例中给出的方式读取您的输入文件时,这就是我得到的输出。现在您可以尝试将其写入您的 txt/csv 文件。
[['John|Doe|2259153|20171102|UKN|UKN|club1' 'John|Doe|2259153|20171031|123.00|456.00|club1' 'Jane|Doe|5964264|20171101|UKN|UKN|club2' 'Jane|Doe|5964264|20171029|789.00|012.00|club2' 'Joe|Smith|1234564|20171001|345.00|678.00|club3']]
由于两个答案都围绕着使用 find/replace,并没有完全解决问题,我使用以下方法来完成任务:
import csv
infile = open('path', 'r')
outfile = open('path', 'r')
findlist = ['lat', 'long', '.jpg.']
replacelist = ["", "", ""]
rep = dict(zip(findlist, replacelist))
s = infile.read()
for item, replacement in zip(findlist, replacelist):
s = s.replace(item, replacement)
outfile.write(s)