split()转换为int()的字符串列表

时间:2017-07-05 19:36:55

标签: python

我无法获取列表值以让<?php $email = $_POST['email']; if(!isset($error) { $del = ", "; //this is where I have to repeat the post data key/values $data = array( $_POST['email'], //You can replace this with $email $_POST['FirstName'], //You can also replace this with the rest of the variables. It should make it cleaner and faster. $_POST['LastName'], $_POST['Address'], $_POST['City'], $_POST['Country'], $_POST['State'], $_POST['Phone1'], $_POST['password'], ); $file = fopen("Registration.csv", "a"); $headers = "Email, First Name, Last Name, Address, City, Country, State, Phone, Password"; if (0 == filesize( "Registration.csv" )) { fwrite($file, $headers . "\n"); } fputcsv($file, $data); fclose($file); } ?>调用它们。该列表来自用户int()input()split()。代码如下:

append()

给了我一个coords = [] fcoords = input("Enter first coordinate pair:\n") urlat, urlon = fcoords.split() coords.append(urlat) coords.append(urlon) scoords = input("Enter second coordinate pair:\n") lllat, lllon = scoords.split() coords.append(lllat) coords.append(lllon) for coord in coords: print (int(coord))

我可以将ValueError: invalid literal for int() with base 10: '-122.4444'int(coord)切换,但由于我将十进制度转换为dms,这只是额外的工作。使用float(coord)得到没有余数的基数,这就是我想要的!

这里的int()方法不正确,还是隐藏了额外的字符?

4 个答案:

答案 0 :(得分:3)

由于文字是-122.4444,它代表一个浮点数;首先将其转换为float,然后将其转换为int:print(int(float(coord)))

答案 1 :(得分:1)

'-122.4444'不是int,因此您无法将其解析为int。

如果您不想使用float(坐标),您可以这样做:

print (int(coord.split('.')[0]))

这将忽略数字的小数部分,只取整数部分

答案 2 :(得分:0)

你有一个匹配float的字符串,你试图将它转换为int,int不支持。正如其他人建议使用int(float(your_str))。

所以问题不在于分裂,但是,您也可以使用可能更适合用户输入的regexp,因为它更灵活。

要匹配您可以使用的整数:

<p>
    Your country:
    <select data-bind="options: availableCountries,
                       optionsText: 'name',
                       value: selectedCountry,
                       optionsCaption: 'Choose...'"></select>

</p>
<div data-bind="visible: selectedCountry">
    <!-- Appears when you select something -->
    You have chosen a country: 
    <span data-bind="text: selectedCountry() ? selectedCountry().name : 'unknown'"></span>.
</div>

@section scripts
{
    <script type="text/javascript">

        $(function () {

        });

        // Constructor for an object with two properties
        var Country = function (name, isocode) {
            this.name = name;
            this.isocode = isocode;
        };

        var viewModel = {
            availableCountries: ko.observableArray([
                new Country("UK", "isoUK"),
                new Country("USA", "isoUSA"),
                new Country("Sweden", "isoSweden")
            ]),
            selectedCountry: ko.observable() // Nothing selected by default
        };


    </script>

}

编辑:

正则表达式可能无法正常运行,您必须进行测试

答案 3 :(得分:-1)

您无法将float字符串更改为int

将您的split()更改为:

urlat, urlon = fcoords.split('.')

lllat, lllon = scoords.split('.')

您的代码可以使用。