Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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-文本文档中的赋值_Python_File_Search_Variable Assignment - Fatal编程技术网

python-文本文档中的赋值

python-文本文档中的赋值,python,file,search,variable-assignment,Python,File,Search,Variable Assignment,我有一个文件,它由不同的值组成。我需要定义它们,然后搜索它们 在文件中我有-编号,编号2,工作类型,名字,姓氏 我怎样才能让它在孔文件中搜索该名称或编号,然后在该行中显示其他变量 现在我所能做的就是打开文件:,我被难住了 import csv fname = input(open("Please enter the name of the file you wish to open: ") 我使用Python3.0只需从开始 结果: Spam, Spam, Spam, Spam, Spam,

我有一个文件,它由不同的值组成。我需要定义它们,然后搜索它们

在文件中我有-编号,编号2,工作类型,名字,姓氏

我怎样才能让它在孔文件中搜索该名称或编号,然后在该行中显示其他变量

现在我所能做的就是打开文件:,我被难住了

import csv

fname = input(open("Please enter the name of the file you wish to open: ")
我使用Python3.0

只需从开始

结果:

Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam
就你而言:

import csv

value1 = "Spam"

fname = input(open("Please enter the name of the file you wish to open: ")

with open(fname, 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in spamreader:
        if value1 in row: # if the string of value1 is exactly one of the cells of the row
            print " ".join(row) # printout the full row
如果你的家庭作业做得很好,并且仔细阅读了强大的文档,你会发现有比我建议的更好的方法来实际使用csv(尽管从我给出的开始,你可以让一些东西工作得很好)


但在这里,我不是要为您编写代码,我只是想给您一个提示,让您可以开始编码!

文本文档或csv文件???您应该尝试一些解决方法。如果您仍然被困,请阅读更多文档或示例。我不是说这是一个家庭作业,我只是告诉您通过阅读文档来完成家庭作业,其中所有答案是!如果with语法导致错误,您应该检查您的python版本,很可能您没有使用最新版本的python。
python--version
应该是2.7或3.0左右
import csv

value1 = "Spam"

fname = input(open("Please enter the name of the file you wish to open: ")

with open(fname, 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in spamreader:
        if value1 in row: # if the string of value1 is exactly one of the cells of the row
            print " ".join(row) # printout the full row