Skip to content
GitLab
    • Explore Projects Groups Snippets
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • A awesome-python
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 13
    • Issues 13
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 317
    • Merge requests 317
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Vinta Chen
  • awesome-python
  • Merge requests
  • !280

Specify encoding explicitly

  • Review changes

  • Download
  • Email patches
  • Plain diff
Closed Administrator requested to merge github/fork/myd7349/master into master 10 years ago
  • Overview 0
  • Commits 1
  • Pipelines 0
  • Changes 1

Created by: myd7349

Since the default system encoding of my Win7 x64(simplified Chinese) is not utf-8, but GBK; sort.py doesn't work in Python 3.4.2.

The traceback message is:

Traceback (most recent call last): File "E:\GitHub\sort.py", line 52, in main() File "E:\GitHub\sort.py", line 22, in main read_me = read_me_file.readlines() UnicodeDecodeError: 'gbk' codec can't decode byte 0x93 in position 6947: illegal multibyte sequence

So I just specify the encoding explicitly by passing the keyword argument 'encoding'. (Otherwise, Python 3.x will use the system's default encoding.)

The builtin 'open' function in Python 2.7.8 doesn't provide such a parameter, however. So I use codecs.open instead.

The modified version has already been tested on Win7 x64 with both Python 2.7.8 and Python 3.4.2.

Compare
  • master (base)

and
  • latest version
    0ce698cc
    1 commit, 2 years ago

1 file
+ 7
- 2

    Preferences

    File browser
    Compare changes
sort.py
+ 7
- 2
  • View file @ 0ce698cc


@@ -15,8 +15,13 @@
def main():
import sys
if sys.version_info.major == 2:
global open
from codecs import open
# First, we load the current README into memory as an array of lines
with open('README.md', 'r') as read_me_file:
with open('README.md', 'r', encoding = 'utf-8') as read_me_file:
read_me = read_me_file.readlines()
# Then we cluster the lines together as blocks
@@ -39,7 +44,7 @@ def main():
blocks.append([line])
last_indent = None
with open('README.md', 'w+') as sorted_file:
with open('README.md', 'w+', encoding = 'utf-8') as sorted_file:
# Then all of the blocks are sorted individually
blocks = [''.join(sorted(block, key=lambda s: s.lower())) for block in blocks]
# And the result is written back to README.md
0 Assignees
None
Assign to
0 Reviewers
None
Request review from
Labels
3
Breaking change (with fallback) Enhancement: General Issue: Bug
3
Breaking change (with fallback) Enhancement: General Issue: Bug
    Assign labels
  • Manage project labels

Milestone
3.2.0
3.2.0 (expired)
None
Time tracking
No estimate or time spent
Lock merge request
Unlocked
0
0 participants
Reference: OpenAPITools/openapi-generator!251
Source branch: github/fork/myd7349/master

Menu

Explore Projects Groups Snippets