简体   繁体   English

如何在 Python 中为 csv.reader 设置语言环境?

[英]How to set locale for csv.reader in Python?

In Python, when we use csv.reader with quoting=csv.QUOTE_NONNUMERIC , it converts unquoted fields into float as specified in the documentation:在 Python 中,当我们将csv.readerquoting=csv.QUOTE_NONNUMERIC csv.reader一起使用时,它会将未加引号的字段转换为文档中指定的浮点数:

Each row read from the csv file is returned as a list of strings.从 csv 文件读取的每一行都作为字符串列表返回。 No automatic data type conversion is performed unless the QUOTE_NONNUMERIC format option is specified (in which case unquoted fields are transformed into floats).除非指定了QUOTE_NONNUMERIC格式选项(在这种情况下未加引号的字段被转换为浮点数),否则QUOTE_NONNUMERIC执行自动数据类型转换。

The code I wrote looks like this:我写的代码是这样的:

with open(file_path, 'r') as file:
    csv_reader = csv.reader(file, quoting=csv.QUOTE_NONNUMERIC)
    header = next(csv_reader)

    # Read line by line
    while line := next(csv_reader):
        # Further processing here

The number conversion process works fine when the file has the same locale as my default one, en_GB .当文件与我的默认语言环境相同时,数字转换过程工作正常, en_GB But if data in the file use comma as the decimal separator ( de_DE locale), the code will break because it cannot convert that string into a float.但是如果文件中的数据使用逗号作为小数点分隔符( de_DE语言环境),则代码将中断,因为它无法将该字符串转换为浮点数。

ValueError: could not convert string to float: '0,761843944084108' ValueError:无法将字符串转换为浮点数:'0,761843944084108'

So, how can I tell the csv.reader which locale to use?那么,我如何告诉csv.reader使用哪个语言环境? I tried using locale.setlocale(locale.LC_ALL, 'de_DE') before opening the file but somehow it doesn't recognize it and I still got the same error.我在打开文件之前尝试使用locale.setlocale(locale.LC_ALL, 'de_DE')但不知何故它无法识别它,我仍然遇到相同的错误。

An example CSV with de_DE looks like this:带有de_DE的示例 CSV 如下所示:

"ID";"Measurement";"Note"
"1";0,23;"Example Value"
"2";1,5;"Another Note"

This file will cause ValueError because 0,23 is not a number in en_GB locale.此文件将导致ValueError因为0,23不是en_GB语言环境中的数字。

What is the proper way to set locale for the csv.reader ?csv.reader设置语言环境的正确方法是什么?

A method that can help, is while processing that data, to use the function:一种可以提供帮助的方法是在处理该数据时使用该函数:

import locale

locale.atof(input)

If your locale is set to de for that file in order to handle the values, you can also find more about that function and more options here .如果您将该文件的语言环境设置为de以处理这些值,您还可以在此处找到有关该函数和更多选项的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM