2026년 11월 10일 화요일

Where did 00123 lose its zeros: pandas input, CSV export, or Excel import?

Check the identifier in the DataFrame, then in the CSV as plain text, then in Excel’s import preview. The first place 00123 becomes 123 tells you which step to fix. A value already reduced to 123 does not reveal how many zeros it originally had. This is a documentation-based comparison; no pandas or Excel run was recorded.

Where did 00123 lose its zeros: pandas input, CSV export, or Excel import?
A reading map of the checks explained below.

If the DataFrame already contains 123, fix the input

Inspect the value in the exact DataFrame passed to export. If the source CSV contains 00123 but the DataFrame contains 123, read the identifier column as text: pd.read_csv("input.csv", dtype={"code": str}). Check the resulting value before exporting. pandas documents per-column dtype selection and says column types can otherwise be inferred. This remedy applies to a nonmissing identifier: recognized missing values can still be parsed as missing despite the requested dtype.

If the DataFrame was assembled from an integer or another source that had already lost the zeros, changing read_csv or to_csv cannot recover the original spelling. Return to the original record. A known fixed width may support a display convention, but do not infer an unknown identifier from 123.

Sources: pandas.read_csv — pandas.read_csv — dtype

If the DataFrame has 00123, inspect the CSV itself

Open the exported CSV in a plain text editor before opening it in Excel. For the illustrative identifier, the expected CSV text is the following two literal lines, with the first line as the header: code 00123 This is an expected comparison point, not tested output. You may also inspect df.to_csv(index=False) for the DataFrame being exported; pandas documents that to_csv returns CSV text when no output path is supplied. Compare the same record in that text and in the saved file.

Use this reading map to choose the next action. It is a diagnostic comparison, not an experimental result. If the DataFrame contains 00123 but the raw CSV says 123, check that the object exported is the object you inspected and look for changes between inspection and export. If the raw CSV still says 00123, preserve that original file for a fresh Excel import.

If the DataFrame has 00123, inspect the CSV itself
First place 00123 becomes 123Likely step to investigateAction
DataFrame; source CSV still has 00123pandas inputRead the nonmissing identifier column as text and inspect it again
Raw CSV; exported DataFrame had 00123Export path or intervening changeConfirm the exported object and inspect the actual saved CSV
Excel; raw CSV still has 00123Excel conversion on importRestart import from the original CSV and retain the column as Text

Sources: pandas.read_csv — pandas.read_csv — dtype, pandas.DataFrame.to_csv — pandas.DataFrame.to_csv — Returns, Keeping leading zeros and large numbers — Keeping leading zeros and large numbers — Set automatic data conversions

If only Excel shows 123, protect the original text during import

Excel can convert numerical text to a number and remove leading zeros. Start from the original CSV that still contains 00123. In Excel, use Data > From Text/CSV and examine the import preview before loading. Verify that the original digits remain in the import data. Microsoft documents opening the editor and setting the identifier column’s data type to Text before Close & Load. If the preview or an earlier import step has already reduced the value to 123, restart from the original CSV; selecting Text after conversion cannot restore the missing digits.

Microsoft also describes Automatic Data Conversions controls for Excel for Microsoft 365 and Excel 2024, including their Mac editions. Availability depends on version. A custom number format can display a known width, but Microsoft says it does not restore zeros removed before formatting. For identifiers with unknown original width, recover the original text rather than applying a guessed format.

Sources: Keeping leading zeros and large numbers — Keeping leading zeros and large numbers — Convert numbers to text when you import text data, Keeping leading zeros and large numbers — Keeping leading zeros and large numbers — Set automatic data conversions, Keeping leading zeros and large numbers — Keeping leading zeros and large numbers — Use a custom format to keep the leading zeros

Primary sources checked: 2026-11-10. Prepared with AI assistance and a separate evidence review. This is documentation-based guidance, not a hands-on test or performance benchmark.

2026년 11월 2일 월요일

When should I use store_true, store_false, or BooleanOptionalAction?

Use store_true for a single switch that turns a feature on, store_false for a single switch that turns it off, and BooleanOptionalAction when users should see both positive and negative forms of the same setting. These actions take no true or false value after the option. Their defaults and Python version requirements determine which interface is appropriate.

When should I use store_true, store_false, or BooleanOptionalAction?
A reading map of the checks explained below.

Choose by the command users should type

If the normal state is disabled, a positive --enabled switch with action='store_true' is direct: omission yields False and presence yields True. For an option named --disable-cache, action='store_true' would set disable_cache to True, which may be exactly what the program needs, although the stored name describes disabling rather than cache state. Choose the destination and help wording so the value has a clear meaning.

If the normal state is enabled and one switch should turn it off, use a negative name such as --no-cache with action='store_false', optionally setting dest='cache'. Omission then gives cache=True and presence gives cache=False. This is useful when only the disabling operation needs a command-line spelling.

Sources: argparse — Parser for command-line options, arguments and subcommands — action; dest

Compare the three interfaces

BooleanOptionalAction suits a setting users may deliberately enable or disable on separate invocations. Adding --cache creates --cache and --no-cache, which store True and False. The documented class was added in Python 3.9, so choose another interface when supporting older Python versions. Unlike the two store actions, its documented default is not an opposite boolean; specify default=True or default=False when the application has a settled starting state.

This table assumes the declarations shown and no parser-wide override. It compares the resulting cache attribute so the choice is about user-facing syntax and omitted-option meaning, not about different variable names.

Compare the three interfaces
DeclarationOmittedAccepted flag and result
add_argument('--cache', action='store_true')False--cache gives True
add_argument('--no-cache', dest='cache', action='store_false')True--no-cache gives False
add_argument('--cache', action=argparse.BooleanOptionalAction, default=None)None--cache gives True; --no-cache gives False

Sources: argparse — Parser for command-line options, arguments and subcommands — action; default; class argparse.BooleanOptionalAction

Set omission and conflict rules deliberately

A flag is complete by itself. Supplying --cache false to one of these declarations does not convert false into a boolean; with no other positional argument to receive it, parse_args reports an unrecognized argument. If callers must pass a text value after --cache, use the strict converter in the companion article instead.

Pick an omitted-option value for the application, not merely for convenience. A boolean default says what happens when the user says nothing. A None default can preserve the distinction between no command-line preference and an explicit positive or negative choice, which is useful when a later configuration source supplies the baseline. If both directions are exposed with BooleanOptionalAction, decide how the application should handle both forms in one invocation; for an interface that must reject conflicting choices, define separate options inside a mutually exclusive group. Argparse documents that such a group rejects simultaneous options. Review the generated help so users can see the accepted spelling and the effect of omission.

Sources: argparse — Parser for command-line options, arguments and subcommands — default; class argparse.BooleanOptionalAction; Mutual exclusion, Argparse Tutorial — Introducing Optional arguments

Primary sources checked: 2026-11-02. Prepared with AI assistance and a separate evidence review. This is documentation-based guidance, not a hands-on test or performance benchmark.

Read the companion guide