Reputation: 11919
I'm new to python so forgive me if this sounds simple. I want to join a few variables to produce a path. Like this:
AAAABBBBCCCC\2\2014_04\2014_04_01.csv
Id + '\' + TypeOfMachine + '\' + year + '_' + month + '\' + year + '_' + month + '_' + day + '.csv'
How do I concatenate this? I putted single quotes around underline or backslash, but stackoverflow omits/modifies them.
Upvotes: 12
Views: 33797
Reputation: 23
You can also use normal strings like:
Id + '/' + TypeOfMachine + '/' + year + '_' + month + '/' + year + '_' + month + '_' + day + '.csv'
Upvotes: 0
Reputation: 1331
You can simply call the character by its ASCII code. (I'm using Python 3.7).
Example:
In this case, the ASCII code is 92, you can use Python's chr() function to call the character
In this website you can find a list of ASCII codes for more printable characters.
The code used above:
delimiter = chr(92)
FileName = 'Id' + delimiter + 'TypeOfMachine' + delimiter + 'year' + '_' + 'month' + delimiter + 'year' + '_' + 'month' + '_' + 'day' + '.csv'
print(FileName)
Hope it helps.
Upvotes: 2
Reputation: 77099
A backslash is commonly used to escape special strings. For example:
>>> print "hi\nbye"
hi
bye
Telling Python not to count slashes as special is usually as easy as using a "raw" string, which can be written as a string literal by preceding the string with the letter 'r'.
>>> print r"hi\nbye"
hi\nbye
Even a raw string, however, cannot end with an odd number of backslashes. This makes string concatenation tough.
>>> print "hi" + r"\" + "bye"
File "<stdin>", line 1
print "hi" + r"\" + "bye"
^
SyntaxError: invalid syntax
There are several ways to work around this. The easiest is using string formatting:
>>> print r'{}\{}'.format('hi', 'bye')
hi\bye
Another way is to use a double-backslash in a regular string to escape the second backslash with the first:
>>> print 'hi' + '\\' + 'bye'
hi\bye
But all of this assumes you're facing a legitimate need to use backslashes. If all you're trying to do is construct Windows path expressions, just use os.path.join
.
Upvotes: 16
Reputation: 112
Without importing os.path module you could simply do:
my_path = '\\'.join([Id,TypeOfMachine, year + '_' + month, year + '_' + month + '_' + day + '.csv'])
Upvotes: 1
Reputation: 1121714
Normally, you'd double the backslash:
'\\'
Use os.path.join()
to join directory and filename elements, and use string formatting for the rest:
os.path.join(Id, TypeOfMachine, '{}_{}'.format(year, month),
'{}_{}_{}.csv'.format(year, month, day))
and let Python take care of using the correct directory separator for your platform for you. This has the advantage that your code becomes portable; it'll work on an OS other than Windows as well.
By using string formatting, you also take care of any non-string arguments; if year
, month
and day
are integers, for example.
Upvotes: 5
Reputation: 2377
You should use os.path.join
to construct the path.
e.g:
import os
path = os.path.join(Id, TypeOfMachine, year + '_' + month, year + '_' + month + '_' + day + '.csv')
or if you insist on using backslashes, you need to escape them: as, so '\\'
Upvotes: 12