我有以下代码:
os.listdir("staging")
# Seperate filename from extension
sep = os.sep
# Change the casing
for n in os.listdir("staging"):
print(n)
if os.path.isfile("staging" + sep + n):
filename_one, extension = os.path.splitext(n)
os.rename("staging" + sep + n, "staging" + sep + filename_one.lower() + extension)
# Show the new file names
print ('\n--------------------------------\n')
for n in os.listdir("staging"):
print (n)
# Remove the blanks, -, %, and /
for n in os.listdir("staging"):
print (n)
if os.path.isfile("staging" + sep + n):
filename_zero, extension = os.path.splitext(n)
os.rename("staging" + sep + n , "staging" + sep + filename_zero.replace(' ','_').replace('-','_').replace('%','pct').replace('/','_') + extension)
# Show the new file names
print ('\n--------------------------------\n')
for n in os.listdir("staging"):
print (n)
"""
In order to fix all of the column headers and to solve the encoding issues and remove nulls,
first read in all of the CSV's to python as dataframes, then make changes and rewrite the old files
"""
import os
import glob
import pandas as pd
files = glob.glob(os.path.join("staging" + "/*.csv"))
print(files)
# Create an empty dictionary to hold the dataframes from csvs
dict_ = {}
# Write the files into the dictionary
for file in files:
dict_[file] = pd.read_csv(file, header = 0, dtype = str, encoding = 'cp1252').fillna('')
Run Code Online (Sandbox Code Playgroud)
在字典中,数据帧被命名为"文件夹/名称(csv)",我想要做的是从字典中的键中删除前缀"staging /".
我怎样才能做到这一点?
cs9*_*s95 15
如果您只想将文件路径截断为文件名,则可以使用os.path.basename:
for file in files:
fname = os.path.basename(file)
dict_[fname] = (pd.read_csv(file, header=0, dtype=str, encoding='cp1252')
.fillna(''))
Run Code Online (Sandbox Code Playgroud)
例:
os.path.basename('Desktop/test.txt')
# 'test.txt'
Run Code Online (Sandbox Code Playgroud)
本着与截断文件路径相同的精神,使用python 标准库中的pathlib 。它将把路径变成一个易于使用的类。
from pathlib import Path
path = Path('Desktop/folder/test.txt')
path.name # test.txt
path.stem # test
path.suffix # .txt
path.parent.name # folder
path.parent.parent.name # Desktop
Run Code Online (Sandbox Code Playgroud)
import os
pathname ='c:\\hello\\dickins\\myfile.py'
head, tail = os.path.split(pathname)
print head
print tail
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20198 次 |
| 最近记录: |