Pergunta

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.

Foi útil?

Solução

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.

Outras dicas

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 '\\'

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.

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

enter image description here

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.

Without importing os.path module you could simply do:

 my_path = '\\'.join([Id,TypeOfMachine, year + '_' + month, year + '_' + month + '_' + day + '.csv'])

You can also use normal strings like:

Id + '/' + TypeOfMachine + '/' + year + '_' + month + '/' + year + '_' + month + '_' + day + '.csv'
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top