标签: io

read命令的第二个参数是什么意思?

我有这个Python代码:

for name, age in read(file, ('name','age')):
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下它的意思吗?

python io syntax

-3
推荐指数
1
解决办法
96
查看次数

如何将用户输入作为浮点数?

我正在运行Python 2.7.10.我正在处理的程序中有以下代码块.

with open('inventory.txt', 'r+') as f:
  inventory = {}
  while True:
    item = raw_input('Item: ')
    inventory[item] = raw_input('Price: ')
    if item == '':
      del inventory['']
      break

  inv = str(inventory)
  f.write(inv).rstrip()
  print inventory
  print inv
  print f.read()
Run Code Online (Sandbox Code Playgroud)

它的作用是提示用户输入项目和价格,然后将所有这些存储为键/值对,然后将该最终字典写入第二个文本文件.然而,在第5行,它似乎是唯一的输入类型,除了有一个字符串.我试图用float()包围raw_input,并尝试使额外的变量无济于事.我能够将raw_input包装在一个int()中,它可以工作,所以它把我扔了.

当我将第5行更改为库存[item] = float(raw_input('Price:'))时,我收到以下错误:

File "C:\Users\Jarrall\Desktop\store\script.py", line 5, in <module>
inventory[item] = float(raw_input('Price: '))
ValueError: could not convert string to float: 
Run Code Online (Sandbox Code Playgroud)

我必须对代码进行哪些更改,以便当用户在第5行输入数值时,它会保存到字典而不是字符串(当前)?

python io input python-2.7

-3
推荐指数
1
解决办法
2万
查看次数

if(x in y)不能在python文件I/O中工作

我正在创建一个高分数据库,到目前为止有两件事情无效.

  1. 如果我输入一个已存在于文件中的名称,它将忽略if语句(在代码中提到)我会对它在哪一行发表评论.

  2. 当我输入名称和分数时,它将替换highscores.txt文件中的当前名称和分数.

这是我的档案:

name = str(input("Enter your name: "))
score = str(input("Enter your score: "))


file = open("N:\highscores.txt", "r")
if(name in file):
    print("You have already entered a score.")
    file.close()
else:
    file = open("N:\highscores.txt", "w")
    file.writelines(name + " : " + score + "\n")
    file.close()
Run Code Online (Sandbox Code Playgroud)

此外,当我解决这个问题时,我如何按大小顺序订购它们?例如:1.450 2. 300等

谢谢.

python io file

-4
推荐指数
1
解决办法
82
查看次数

从文件中读取最后20个字节

int fd, read_byte;
char *c;  

fd = open("foo.txt", O_RDONLY);

read_byte = read(fd, c, 20);

printf("");
Run Code Online (Sandbox Code Playgroud)

如何从文件中读取最后20个字节,以及如何将read_byte打印到屏幕上。

c io

-4
推荐指数
1
解决办法
3438
查看次数

使用数字文件内容作为int(Haskell)

有没有什么方法可以使用文件的内容,它包含一个计数器值(数据库系统的增量id)作为常规int?

我们正在尝试在Haskell中创建一个小型数据库系统,并需要跟踪表的增量ID.我们认为唯一"简单"的方法是将它们保存在普通的txt文件中.但是,在检索它们时,返回类型将是IO Int.我们的数据库表定义需要一个Intid作为id.

有没有人知道如何使用txt文件中的增量id(或一般的整数值)来获取需要Int值的类实例?

提前致谢!

编辑:说明问题的代码示例:

data SomeTable = SomeTable { id :: Int, testField :: String }
someRow = SomeTable (generateNextId "SomeTable") "Test"
Run Code Online (Sandbox Code Playgroud)

where generateNextId从文件中读取当前增量id(因此具有类型IO Int而不是Int).

database io int haskell types

-4
推荐指数
1
解决办法
62
查看次数

C++ 在使用 cout 和 printf 时不四舍五入

我需要制作一个计算 cos(x) 的程序,我的问题是,当我使用printf例如 cos(0.2) 是 0.98 但结果是 0.984 并且它没有四舍五入到 2 个数字。

我的代码:

#include <iostream> 
#include <math.h>

using namespace std;

int main()
{
    float x = 0.2;
    cout << "x=" << x << " cos(y) y=" << printf("%.2f", cos(x)) << "\n";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ io

-4
推荐指数
1
解决办法
84
查看次数

为什么模数运算符给出了错误的答案?

#include<iostream>
#include<string>
#include<sstream>

using namespace std;

int main(){
    stringstream ss;
    ss << 32;
    string str = ss.str();
    cout << str << endl
         << str[0] << endl
         << str[1] <<endl
         << str[0]%10;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是:

32
3
2
1

相反,最后一行应为3,因为3%10 = 3.

c++ string io stringstream mod

-5
推荐指数
1
解决办法
449
查看次数

C++23 `print` 是否检查写入是否成功进入流?

我想知道标准委员会是否已经修复了臭名昭著的Hello, world! 漏洞。我主要谈论新<print>库(尚未在任何编译器中使用)。

{fmt}库(它启发了标准库)尚未修复此问题。显然,它在输出时不会抛出任何异常/dev/full(从 v9.1.0 开始)。因此,使用 CI/O 函数(例如std::fflush错误处理)仍然是一件事。

下面的程序注意到错误并返回失败代码(因此没有错误):

#include <exception>
#include <cstdio>
#include <cstdlib>
#include <fmt/core.h>


int main()
{
    fmt::println( stdout, "Hello, world!" );
    if ( std::fflush( stdout ) != 0 || std::ferror( stdout ) != 0 ) [[unlikely]]
    {
        return EXIT_FAILURE;
    }
}
Run Code Online (Sandbox Code Playgroud)

但这在 C++23 中可能吗?

#include <print>
#include <exception>
#include <cstdio>
#include <cstdlib>


int main()
{
    try
    {
        std::println( stdout, "Hello, world!" );
    }
    catch ( const std::exception& …
Run Code Online (Sandbox Code Playgroud)

c++ io error-handling fmt c++23

-5
推荐指数
1
解决办法
576
查看次数

PHP代码不起作用

我正在尝试创建一个我的网站访问计数器,我做了这样的事情:

contatore.txt:0

index.html的:

<html><head>
</head>
<body>
<p><?php include ("counter.php"); ?></p>
</body></html>
Run Code Online (Sandbox Code Playgroud)

counter.php:

<?php
    $file_handle = fopen("contatore.txt", "r");
    $line = ((int)(fgets($file_handle))) + 1;
    fclose($file_handle);
    $fh = fopen( 'filelist.txt', 'w' );
    fwrite($fh, (string)($line));
    fclose($fh);
    echo ((string)($line));
?>
Run Code Online (Sandbox Code Playgroud)

这是问题:浏览器只是自动隐藏PHP代码使用:(你能帮助我吗??谢谢

php io counter input output

-10
推荐指数
1
解决办法
64
查看次数

标签 统计

io ×9

c++ ×3

python ×3

input ×2

c ×1

c++23 ×1

counter ×1

database ×1

error-handling ×1

file ×1

fmt ×1

haskell ×1

int ×1

mod ×1

output ×1

php ×1

python-2.7 ×1

string ×1

stringstream ×1

syntax ×1

types ×1