【问题标题】:Split string using delimiter “\” in python windows在 python 窗口中使用分隔符“\”拆分字符串
【发布时间】:2020-04-29 17:20:55
【问题描述】:

我在获取只有 ID 名称的列时遇到问题。 Id 名称不应包含任何附加到它的路径。该代码似乎在 Ubuntu 中运行良好,但在 Windows 中却不行。这是代码sn-p:

pd.DataFrame({"patientId": [each_train_dicom.split("/")[-1].split(".")[0]]

【问题讨论】:

  • 什么是each_train_dicom?您还说代码在 Windows 上不起作用,您的体验是什么(即错误、意外结果等)
  • 你能提供一个工作(字符串)的例子吗?
  • for i, each_train_dicom in enumerate(list_of_train_dicoms): this.谢谢回复,问题已经解决了。

标签: python pandas dataframe split operating-system


【解决方案1】:

您可以使用os.path.sep 作为分隔符,而不是硬编码“\”

import os

s = r"C:\Windows\Folder\file.txt"
print(s.split(os.path.sep))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 2013-06-27
    • 2012-03-01
    相关资源
    最近更新 更多