Python 3.5.1从文件中读取

时间:2016-02-05 14:46:35

标签: python-3.5

我已经为学校写了一个数学测验,最后的任务要求我从文件中取得分数并以各种方式订购。我正在执行我认为只是写入文件的简单任务,但事实证明它不起作用。有人可以告诉我这件事中缺少什么。

with open("Class A.txt", "r") as f:
    list(f)

2 个答案:

答案 0 :(得分:0)

如果文件已经是列表格式,即[1,2,3,4,5],您只需使用eval函数即可。你可以尝试下列之一:

mysqli_*

如果你的字符串是这样的“6 | Louis | Perry 2 | Shiro | Ski 4 | A | B”,你可以做到

<?php
//connect to the database using mysqli
$name=$_FILES["image"]["name"];
$type=$_FILES["image"]["type"];
$size=$_FILES["image"]["size"];
$temp=$_FILES["image"]["tmp_name"];
$error=$_FILES["image"]["error"];
if($error>0)
    die("error while uploading");
else
{
    //Insert the informations in your database here, so even without the upload, the info about the file will be stored
    $query = mysqli_query("INSERT INTO blogs(image)values('$name')");
    if($type == "image/png" || $type == "image/jpg"|| $type == "image/jpeg" || $type == "image/svg" || $type == "image/jpe" || $type==" ")
    {
        move_uploaded_file($temp,"upload/".$name);
        echo "upload complete"; 
    }
    else
    {   
        die("Format not allowed or file size too big!");
    }
}

答案 1 :(得分:0)

这个怎么样:

>>> with open("Class A.txt", "r") as f:
...     content = f.read()
...     content_list = content.split('\n')  # split on space/comma/...

阅读内容,然后使用您使用的分隔符将其拆分,无论是输入(\ n),逗号还是其他任何内容。