python - Assigning multi-line raw string to variable for use in read_csv
问题描述
I am trying to assign a raw file path to a variable for use in read_csv in python. The ultimate intent is to take file path as an input in a GUI, and use this to run read_csv. The string is very long, and, for the time being, I am just trying to get the string - variable assignment working.
I followed another thread which suggested using r'''drive:\yada\yada...''' however this adds an additional "\" to each step in the file path. Any suggestions for how to prevent this? Also, any suggestions on best approach to take a file path as input to a GUI and use this to read_csv would be greatly appreciated.
Example of problem below...
In[219]: pathProject = r'''C:\Users\Account\OneDrive\
\Documents\Projects\2016\Shared\
\Project-1\Administrative\Phase-1\
\Final'''
In[220]: pathProject
Out[220]: 'C:\\Users\\Account\\OneDrive\\\n\\Documents\\Projects\\2016\\Shared\\\n\\Project-1\\Administrative\\Phase-1\\\n\\Final'
解决方案
如果您想通过将长字符串拆分为多行来输入一个长字符串,您可以利用 Python 的字符串连接。由于您想在多行中输入它,它们必须包含在括号中,例如:
pathProject = (r"C:\Users\Account\OneDrive"
r"\Documents\Projects\2016\Shared"
r"\Project-1\Administrative\Phase-1"
r"\Final")
print(pathProject)
# C:\Users\Account\OneDrive\Documents\Projects\2016\Shared\Project-1\Administrative\Phase-1\Final
请注意左括号和右括号,并且必须将字符串的每个部分声明为原始字符串。
推荐阅读
- c - 循环打印输入值以及正确的输出
- java - 在 Spring Cloud Gateway 中获取真实的客户端 IP 地址
- python - Python:每次运行py文件时以编程方式生成增量序列号
- python - 将经度和纬度保存到 Django 中的用户配置文件
- spring - 有没有办法从 BeanFactoryPostProcessor 中读取属性文件?
- excel - 从上周五下午 2 点开始。Excel公式
- python - Python Pandas:如何对组条形图进行排序?
- c# - 实例与方法作为参数——理解生命周期和垃圾收集
- arrays - 下标值既不是数组也不是指针也不是向量
- python - 在没有 Conda 的情况下安装 jupyter lab 扩展