用Python加载文件。使字符串生

时间:2018-04-18 14:37:41

标签: python load

我想使用某个包在Python中加载文件。

包的加载功能需要知道文件名(+扩展名)和位置。像这样:

package.load(os.path.join(my_directory_name, r'my_filename.xml'))

我的情况是我从pandas数据帧中获取文件名。然后我添加正确的扩展名,即xml然后我尝试加载该文件。

import os
import pandas as pd

package.load(os.path.join(directory_name, df['filename'] + '.xml'))

我收到错误:目录中没有此类文件。

问题在于r不存在。它应该在字符串的前面。如何制作字符串df [' filename] +' .xml'生的? Python将反斜杠解释为特殊字符,但我希望它们代表实际的反斜杠。

2 个答案:

答案 0 :(得分:0)

试试这个,它应该可行

var specs = require('../public/javascripts/specs');

答案 1 :(得分:0)

我用过这个:

escape_dict = {'\'': r'\'', '\"': r'\"'}

def _raw(text):
    """Returns a raw string representation of text"""
    new_string = ''
    for char in text:
        try:
            new_string += escape_dict[char]
        except KeyError:
            new_string += char
    return new_string

如果您现在放_raw(my_string),它将起作用。