Хранение обозначений денежной единицы в таблице базы данных

Мне всегда нравится обрабатывать файлы, используя конвейерный подход, таким образом, вы можете использовать параллелизм, если ваши входные данные становятся действительно большими. В любом случае, вы можете легко проверить производительность, используя %timeit, если вы используете ipython, но вот что я бы сделал:

processed = ""

def replace_char(line, char, replacement):
    return line.replace(char, replacement)

with open('SOME_PATH') as fh:
    processed += replace_char(replace_char(fh.read(), ":", " "), ",", "")

print(processed)

# OUTPUT
# 06 38 34 16.09.2017  739648.41186077976.8575 54.791616 12.727939
# 06 38 35 16.09.2017  739647.06286077975.6925 54.791606 12.727917

С этим подходом, если вы хотите внести изменения в способ обработки файл все, что вам нужно сделать, это изменить replace_char или написать другую функцию, если хотите. Если вам нужен параллелизм, вы можете использовать пакеты multiprocessing или asyncio.

6
задан 23 April 2009 в 23:43
поделиться

3 ответа

Issues with this approach

If you need to output currency text, I would recommend that you do not store the currency symbol associated with the currency data. Even if the symbol is stored in another column and you pull it out as needed. Rather a better approach would be to store the region / locale for the currency data then, format the currency using the regions default formatting. If whatever language/platform you are using doesn't have locale based currency functionality, I still think this is a better approach. In that case, you would do the formatting in a bit of application logic and then display it to the user.

The main reasoning being this is that in different regions and locales not only is the symbol different, but so is the placement of the symbol and the formatting of the currency itself. Also think if you need to distinguish between different types of dollars - maybe at some point you will want to indicate if the dollar amount is in USD, HKD, AUD, etc.

Currency examples

  1.234,56€ - Euro (Germany)
€ 1.234,56  - Euro (Netherlands)
€ 1,234.56  - Euro (Ireland)
  123,456円 - Yen (Japan)
  123,456¥ - Yen (Japan) - yes there are two different currency marks!

Other resources

6
ответ дан 16 December 2019 в 21:46
поделиться

I would point to two main pitfalls:

First, make sure your database is correctly installed as utf-8. Some database force you to chose this at install time, others at schema/user creation time.

Second, make sure the programming language you use supports utf-8. php is especially known to cause problems whith string manipulation and utf-8.

The create database statement for firebird looks as the following:

Make sure you use at least version 2.0 of firebird, the first version to support utf-8.

      CREATE DATABASE <database> 
      USER <username> 
      PASSWORD <password> 
      PAGE_SIZE <pagesize> 
      DEFAULT CHARACTER SET UTF8

Alternatively you can create a single field with

      CREATE TABLE money (
       SYMBOL VARCHAR(50) CHARACTER SET UTF8 )

Edit: To clarify on what to store: you store exactly the characters as they appear on the screen. Most likely you can just cut/paste them from any program to your database or inside a sql statement. No need to fumble around with conversions to int/hex/oct/ascii whatsoever.

Edit2: Of course you do not store the fully formatted number, but rather only the single currency symbols.

2
ответ дан 16 December 2019 в 21:46
поделиться

Это также может быть проблема со шрифтом, некоторые шрифты не имеют всех редких символов utf-8. Например, Arial отсутствует. "Lucida Sans Unicode" определенно имеет это. Попробуйте установить «Lucida Sans Unicode» в своей программе просмотра.

0
ответ дан 16 December 2019 в 21:46
поделиться
Другие вопросы по тегам:

Похожие вопросы: