Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Sava文件中的数据_Python_Python 3.x - Fatal编程技术网

Python Sava文件中的数据

Python Sava文件中的数据,python,python-3.x,Python,Python 3.x,我需要一种方法将一些数据保存到文件中。 它们是弦 # TODO: it's your job to implement this function. You can choose an # appropiate file format (what is important is that the dataset is # easy to load later). Hint: you can for instance use the csv module # (https://docs.pyt

我需要一种方法将一些数据保存到文件中。 它们是弦

# TODO: it's your job to implement this function.  You can choose an
# appropiate file format (what is important is that the dataset is
# easy to load later).  Hint: you can for instance use the csv module
# (https://docs.python.org/3/library/csv.html).
def save_data(data: DataList, file_path: str):
    """Save the given dataset in the given file."""
    pass
有人知道怎么做吗。。。因为我试了很久,但是我做不到

全班

from random import Random
from typing import Dict, List, Tuple


import os
import os.path


def read_lines(path: str) -> List[str]:
    """Return the list of lines in the file under the given path."""
    lines = []
    with open(path, 'r') as f:
        lines = f.readlines()
    return [line.strip() for line in lines]


# Some useful type aliases: languages are represented by strings,
# as well as person names.
Lang = str
Name = str


# Two different ways to represent the dataset:
#
# (a) dictionary mapping languages to the corresponding names
#     (close to the actual representation on disk)
#
# (b) list of pairs (name, language), which is the representation
#     we can actually use for training a machine learning model
#
DataDict = Dict[Lang, List[Name]]
DataList = List[Tuple[Name, Lang]]


def read_names(dir_path: str) -> DataDict:
    """
    Read the dataset in the given directory.  The result is the
    dictionary mapping languages to the corresponding person names.
    """
    data = {}
    for file_path in os.listdir(dir_path):
        full_path = os.path.join(dir_path, file_path)
        lang = os.path.splitext(file_path)[0]
        data[lang] = []
        for name in read_lines(full_path):
            data[lang].append(name)
    return data

# DataDict = Dict[Lang, List[Name]]
# DataList = List[Tuple[Name, Lang]]


# TODO: it's your job to implement this function.
def convert(data: DataDict) -> DataList:
    """
    Convert the dictionary representation to the listc
    of (name, language) pairs.
    """
    for language in data:
        name_list = data[language]
        for name in name_list:
            pair = Tuple[name, language]
            print(DataList)
            DataList.append(pair)

    return DataList


# A toy data dictionary to test our functions
data_dict = {
    "EN": ["Andrew", "Burford", "Downey", "Kilford", "Travis"],
    "DE": ["Adlersflügel", "Brahms", "Günther", "Krüger", "Schulte"]
}


# Some checks to see if conversion works as expected
data_list = convert(data_dict)
assert ('Travis', 'EN') in data_list
assert ('Schulte', 'DE') in data_list
assert ('Adlersflügel', 'EN') not in data_list


# TODO: it's your job to implement this function.
def random_split(data: list, rel_size: float) -> Tuple[list, list]:
    """
    Take a list of elements (e.g., a DataList) and divide it randomly to
    two parts, where the size of the first part should be roughly equal
    to `rel_size * len(data)`.

    Arguments:
    data: list of input elements
    rel_size: the target relative size of the first part
    """
    # Check the input argument
    assert rel_size >= 0 and rel_size <= 1

    first_data = list
    second_date = list

    for item in data:
        if Random.random() < rel_size:
            first_data.add(item)
        else:
            second_date.add(item)

    return Tuple[first_data, second_date]


# Check if random_split works as expected on the toy dataset.
(part1, part2) = random_split(data_list, rel_size=0.5)
# The length of the two parts should be the same (since rel_size=0.5)
assert len(part1) == len(part2)
# Since there are no repetitions, there should be no common elements
# in the two parts
assert not set(part1).intersection(set(part2))


# TODO: it's your job to implement this function.  Hint: you can do that
# with the help of the random_split function.
def three_way_split(data: list, dev_size: float, test_size: float) \
        -> Tuple[list, list, list]:
    """
    Take a list of elements (e.g., a DataList) and divide it randomly to
    three parts (train, dev, test), where the size of the dev part should
    be roughly equal to `dev_size * len(data)` and the size of the test part
    should be roughly equal to `test_size * len(data)`
    """
    # Check the input arguments
    assert dev_size >= 0 and test_size >= 0
    assert dev_size + test_size <= 1

    split_data = random_split(data, dev_size)
    dev_data = split_data[0]  # !!!!!
    split_data = random_split(split_data[1], test_size)
    test_data = split_data[0]
    train_data = split_data[1]

    return Tuple[train_data, dev_data, test_data]


# TODO: it's your job to implement this function.  You can choose an
# appropiate file format (what is important is that the dataset is
# easy to load later).  Hint: you can for instance use the csv module
# (https://docs.python.org/3/library/csv.html).
def save_data(data: DataList, file_path: str):
    """Save the given dataset in the given file."""
    pass


# TODO: combine the implemented functions to actually divide the dataset
# with names to three separate parts.  You can use 80% of the original
# dataset as train and 10% of the original dataset as dev.
从随机导入随机
输入import Dict、List、Tuple
导入操作系统
导入操作系统路径
def read_行(路径:str)->列表[str]:
“”“返回文件中给定路径下的行列表。”“”
行=[]
打开(路径“r”)作为f:
行=f.读行()
return[line.strip()用于行中的行]
#一些有用的类型别名:语言由字符串表示,
#还有人名。
Lang=str
Name=str
#表示数据集的两种不同方式:
#
#(a)将语言映射到相应名称的词典
#(接近磁盘上的实际表示形式)
#
#(b)成对列表(名称、语言),即表示法
#我们实际上可以用它来训练机器学习模型
#
DataDict=Dict[Lang,List[Name]]
DataList=List[Tuple[Name,Lang]]
def read_name(目录路径:str)->DataDict:
"""
读取给定目录中的数据集。结果是
字典将语言映射到相应的人名。
"""
数据={}
对于os.listdir(目录路径)中的文件路径:
完整路径=os.path.join(目录路径、文件路径)
lang=os.path.splitext(文件路径)[0]
数据[lang]=[]
对于读取行中的名称(完整路径):
数据[lang]。追加(名称)
返回数据
#DataDict=Dict[Lang,List[Name]]
#DataList=List[Tuple[Name,Lang]]
#TODO:实现此功能是您的工作。
def转换(数据:DataDict)->数据列表:
"""
将字典表示形式转换为listc
成对的(姓名、语言)。
"""
对于数据中的语言:
名称\列表=数据[语言]
对于名称列表中的名称:
pair=元组[名称,语言]
打印(数据列表)
DataList.append(成对)
返回数据列表
#一个玩具数据字典来测试我们的功能
数据记录={
“EN”:[“安德鲁”、“伯福德”、“唐尼”、“基尔福德”、“特拉维斯”],
“DE”:[“阿德勒斯弗吕格尔”、“勃拉姆斯”、“甘瑟”、“克鲁格”、“舒尔特”]
}
#一些检查以查看转换是否按预期工作
数据列表=转换(数据目录)
在数据列表中断言('Travis','EN')
在数据列表中断言('Schulte','DE')
断言('Adlersflügel','EN')不在数据列表中
#TODO:实现此功能是您的工作。
def random_split(数据:list,rel_size:float)->Tuple[列表,列表]:
"""
获取元素列表(例如,数据列表)并将其随机划分为
两部分,其中第一部分的大小应大致相等
到'rel_size*len(数据)`。
论据:
数据:输入元素列表
相对大小:第一个零件的目标相对大小
"""
#检查输入参数

断言rel_size>=0和rel_size以下是注释中链接的相关部分:

import csv
with open('eggs.csv', 'w', newline='') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=' ',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
    spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
    spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
它应该大致如下所示:

with open(file_path, 'w') as csvfile:
    writer = csv.writer(csvfile, delimiter=',')
    writer.writerows(data)

到底试了什么?即使它不起作用,也要显示你的尝试以及我删除它的数据列表,因为我的教授说它错了。我会上传整个课程谢谢,但是还不清楚你在“保存数据”功能中尝试了什么你看过这篇文章吗?