如何用Python确定日文Excel文件转CSV时适用的分隔符与转义字符?
Nice question! Let’s walk through how to tackle this—from picking the right Python tools to figuring out optimal CSV settings for your Japanese Excel files, plus handling multi-sheet workbooks and prepping for Snowflake.
Your best bet is pandas—it’s robust for handling both .xlsx (Excel 2007+) and OOXML formats, and pairs seamlessly with openpyxl (the go-to engine for modern Excel files) under the hood. Just install the necessary dependencies first:
pip install pandas openpyxl
Since your files contain Japanese text (which might include full-width commas or other symbols), you’ll want to avoid using a delimiter that’s already present in your data. Here’s a practical approach:
- Sample your data: Pull the first few hundred rows from an Excel sheet to analyze character frequency.
- Count candidate delimiters: Test common options (both half-width and full-width, since Japanese uses both) to find the one that appears least often.
- Handle escape characters: Default to double quotes (
") for escaping, but add an escape character if your data already contains quotes.
Here’s a reusable function to find the optimal delimiter:
import pandas as pd def find_best_delimiter(df_sample): # Include both half-width and full-width delimiters common in Japanese data candidate_delimiters = [',', '\t', ';', '|', ',', ';'] delimiter_counts = {} for delim in candidate_delimiters: # Count how many times the delimiter appears across all columns total_occurrences = df_sample.astype(str).apply(lambda col: col.str.contains(delim)).sum().sum() delimiter_counts[delim] = total_occurrences # Return the delimiter with the fewest occurrences return min(delimiter_counts, key=delimiter_counts.get)
For escape characters, check if your data contains double quotes and adjust your CSV export settings accordingly. Pandas works smoothly with the csv module’s constants for this.
To process all sheets in a file, use pandas’ sheet_name=None parameter—it returns a dictionary where keys are sheet names and values are DataFrames. Loop through this to convert each sheet to its own CSV file:
import os import csv def convert_excel_to_csv(excel_path, output_dir): os.makedirs(output_dir, exist_ok=True) # Read all sheets in the Excel file sheets = pd.read_excel(excel_path, sheet_name=None, engine='openpyxl') for sheet_name, df in sheets.items(): # Get the best delimiter for this sheet's data (sample first 200 rows) best_delim = find_best_delimiter(df.head(200)) # Configure CSV export settings csv_settings = { 'sep': best_delim, 'encoding': 'utf-8-sig', # Critical for Japanese text in Snowflake 'index': False } # Check if quotes exist in the data and adjust escaping has_double_quotes = df.astype(str).apply(lambda col: col.str.contains('"')).sum().sum() > 0 if has_double_quotes: csv_settings.update({ 'quotechar': '"', 'escapechar': '\\', 'quoting': csv.QUOTE_MINIMAL }) # Generate a unique filename for the sheet base_filename = os.path.splitext(os.path.basename(excel_path))[0] csv_filename = f"{base_filename}_{sheet_name}.csv" output_path = os.path.join(output_dir, csv_filename) # Export to CSV df.to_csv(output_path, **csv_settings) print(f"Converted sheet '{sheet_name}' to {output_path}") # Example usage convert_excel_to_csv('your_japanese_excel.xlsx', 'csv_output')
- Encoding: Stick with
utf-8-sig—Snowflake handles this encoding flawlessly for Japanese characters, avoiding garbled text. - COPY INTO Configuration: When using Snowflake’s
COPY INTOcommand, mirror the delimiter and escape settings you used. For example:COPY INTO your_table FROM @your_stage/csv_output/ FILE_FORMAT = ( TYPE = CSV FIELD_DELIMITER = '|' # Replace with your chosen delimiter ESCAPE = '\\' FIELD_OPTIONALLY_ENCLOSED_BY = '"' ENCODING = 'UTF8' ); - Schema Matching: Use pandas’
df.dtypesto generate a basic CREATE TABLE statement for Snowflake, ensuring your data types align correctly.
内容的提问来源于stack exchange,提问作者biggboss2019

