Skip to main content

A simple package to processing CJK string

Project description

CJKStr -- A Simple Package for Processing CJK string

In Python, the string formatting function will treat a CJK characters as wide as a english letter.This make the result weired. For example:

print("1234567890" * 4) 
a = "測試"
w = 10
print(f"{a:{w}s}|")

produce output as this:

1234567890123456789012345678901234567890
測試        |

The width is setting to 10 in the formatting string. Since the 2 Chinese characters occupies 2 more spaces, the bar is printed in position 13.

To avoid this problem, we need to know how many CJK Chinese characters in the string and resuce the width accorantly.

This package has only one funcion:

count_cjk_chars(s) # return number of chinese characters in string s

example

import cjkstr

print("1234567890" * 4) 
a = "測試"
w = 10
print(f"{a:{w}s}|")
w = 10 - cjkstr.count_cjk_chars(a)
print(f"{a:{w}s}|")

ouput:

1234567890123456789012345678901234567890
測試        |
測試      |

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cjkstr-0.0.3.tar.gz (1.7 kB view hashes)

Uploaded Source

Built Distribution

cjkstr-0.0.3-py3-none-any.whl (2.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page