This question already has an answer here:
Python 3.4.1 script syntax error, arcpy & [duplicate]
(1 answer)
Closed 7 years ago.
I am very new to Python and am getting this small problem. I am using Python 3.3
There is a variable I declared in my code
file_name = "resource\email\ham\6.txt"
However, when I look for the variable, it appends additional numbers
>>file_name
'resource\\email\\ham\x06.txt'
Is there a reason why it behaves as so? If not, how do I remove those additional characters? Also, why are they there?
Use r raw string:
file_name = r"resource\email\ham\6.txt"
Double \:
file_name = "resource\\email\\ham\\6.txt"
Or /:
file_name = "resource/email/ham/6.txt"
\ has a special meaning in python, it is used to escape characters.
Related
This question already has answers here:
How can I put an actual backslash in a string literal (not use it for an escape sequence)?
(4 answers)
Closed 3 months ago.
While adding an absolute path to my script because it has a \f in it the code won't run properly.
C:\Users\showoi\Desktop\website\repository\fileAdder\softwarelisting.xlsx
The file is in the same directory as the script but using a relative path won't work. No misspellings or anything.
Use python r string
path=r'C:\Users\showoi\Desktop\website\repository\fileAdder\softwarelisting.xlsx'
Use one of the following ways:
r"C:\Users\showoi\Desktop\website\repository\fileAdder\softwarelisting.xlsx"
"C:\\Users\\showoi\\Desktop\\website\\repository\\fileAdder\\softwarelisting.xlsx"
"C:/Users/showoi/Desktop/website/repository/fileAdder/softwarelisting.xlsx
This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 2 years ago.
I cant seem to write something like : "C:\Users\alon4\Desktop\Learning Material\Odyssey\Lab a2\Magnetism\data"
As a string to print or otherwise, i think its something to do with the slashes
There can be 3 ways :
1) Either use r (raw strings) before the actual string :
For example : r"C:\Users\alon4\Desktop\Learning Material\Odyssey\Lab a2\Magnetism\data"
2) Or use // like:
For example : "C:\\Users\\alon4\\Desktop\\Learning Material\\Odyssey\\Lab a2\\Magnetism\data"
3) Use slashes: "C:/Users/alon4/Desktop/Learning Material/Odyssey/Lab a2/Magnetism/data"
(As suggested by Óscar López)
This question already has answers here:
String formatting: % vs. .format vs. f-string literal
(16 answers)
String formatting in Python 3
(5 answers)
Closed 2 years ago.
I have python 3.7.4 and it has f string support. But the server I am porting my data has python < 3.6 and so does not support f string. The following code works fine in local machine:
directory_root = 'dataset_test/'
root_dir = listdir(directory_root)
for animal_folder in root_dir:
animal_folder_list = listdir(f"{directory_root}/{animal_folder}")
But this code fails in server as it does not support f string. How could I rewrite it using format ?
You can write it like this:
listdir("{directory_root}/{animal_folder}".format(directory_root=directory_root, animal_folder=animal_folder))
Or
listdir("{}/{}".format(directory_root, animal_folder))
You can simply use string formatting like following
"Your string {0} {1}...{}".format(variable1, variable2, variable)
The correct way, that works in every environment, not only on your local linux box would be:
animal_folder_list = listdir(os.path.join( directory_root, animal_folder))
When you explicitly specify folder separator (/), it will break on Win-boxes or in other hostile environments. os.path.join() comes to the rescue =)
This question already has answers here:
Convert a Unicode string to a string in Python (containing extra symbols)
(12 answers)
Closed 3 years ago.
I need to convert word
name = 'Łódź'
to ASCII characters
output: 'Lodz'
I can't import any library like unicodedata.
I need to do it in clear python.
I've tried to encode than decode and nothing worked.
Well, a simple method would be to map and replace. This also does not require any special imports.
name = 'Łódź'
name=name.replace('Ł','L')
name=name.replace('ó','o')
name=name.replace('ź','z')
print(name)
This question already has answers here:
String formatting: % vs. .format vs. f-string literal
(16 answers)
Closed 4 years ago.
I am using a Markov chain. When the chain arrives at a particular state, two files (a .png and an .mp3) need to open.
s is the current state of the chain, an integer from 1-59.
I can't seem to find how to open the file with the same number as 's'.
I'm sure it has something to do with %str formatting, but I can't seem to implement it.
img = Image.open('/.../.../s.png')
img.show()
You should use the following line in your code:
img = Image.open('/.../.../{0}.png'.format(s))
You can format a string using a variable like this
>>> s = 10
>>> '/path/to/file/{}.png'.format(s)
'/path/to/file/10.png'