简体   繁体   English

使用 pandas 读取文件(.csv)文件并从中提取文件名

[英]Read a file(.csv) file and extract file name from it using pandas

Read a file and extract the file name from it.读取文件并从中提取文件名。 Need to remove digit and extra charactres from the file and print output.需要从文件中删除数字和多余的字符并打印 output。

For ie:if the file name is 53943_Sreerag_cde_one_check.xlsx" or Sreerag_cde_one_check.xlsx and output should be "Sreerag即:如果文件名是 53943_Sreerag_cde_one_check.xlsx" 或 Sreerag_cde_one_check.xlsx 和 output 应该是 "Sreerag

Code i have tried.我试过的代码。

ini_string = pd.read_csv("D:\wGroup_000005 KutakW Special_RSLRpt_PD2021-06-16_Run_as_2021_6 _on_20210616_PKG_5.csv")
final = ini_string.split("_")
try:
    # Check if the first value is int.
    int(final[0])
    Name=final[1].upper()
    print(Name)
except:
    Name=final[0].upper()
    print(Name)

You don't have to read the file into a dataframe first to get something from the filename.您不必先将文件读入 dataframe 即可从文件名中获取信息。 Perform all your operations on filename directly直接对文件名执行所有操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM