Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.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 扫描多张存储的文件中的单词,并将它们的出现(1或0)保存到一个包含单词的数据框中_Python_Pandas_File_For Loop_Path - Fatal编程技术网

Python 扫描多张存储的文件中的单词,并将它们的出现(1或0)保存到一个包含单词的数据框中

Python 扫描多张存储的文件中的单词,并将它们的出现(1或0)保存到一个包含单词的数据框中,python,pandas,file,for-loop,path,Python,Pandas,File,For Loop,Path,我目前正在进行第一个Python项目,我被困在这一步中。我有一个单词列表(variable=wordlist)和存储在路径中的文件/论文。结果现在应该显示一个矩阵,列=论文名称,行=单词。如果该单词出现在文本中,我希望得到一个“1”,如果不是“0” 这是我的密码: filelists=os.listdir(“路径”) df=pd.DataFrame(列=[filelist],索引=[wordlist]) 对于文件列表中的docPath: doc=io.open(“路径“+”\\\'+docPat

我目前正在进行第一个Python项目,我被困在这一步中。我有一个单词列表(variable=wordlist)和存储在路径中的文件/论文。结果现在应该显示一个矩阵,列=论文名称,行=单词。如果该单词出现在文本中,我希望得到一个“1”,如果不是“0”

这是我的密码:

filelists=os.listdir(“路径”)
df=pd.DataFrame(列=[filelist],索引=[wordlist])
对于文件列表中的docPath:
doc=io.open(“路径“+”\\\'+docPath,'r',编码='utf-8')
文件内容=doc.read()
对于单词列表中的单词:
如果文件内容中有word:
追加({'filelists':1},忽略_index=True)
其他:
追加({'filelists':0},忽略_index=True)
文件关闭()
打印(df)
当我运行代码时,我得到TypeError:Expected tuple,get str

如果我将值1和0放入tuple()中,它将解决此错误

if word in file_content:
    df = df.append({'filelists': tuple(1)}, ignore_index=True)
else:
    df = df.append({'filelists': tuple(0)}, ignore_index=True)
但是,我现在收到以下错误

类型错误:“int”对象不可编辑


这里有人能帮我吗?正如您可能看到的,我对编码是新手,因此如果我在代码中犯了一些完全错误,请原谅。

我认为您的代码中有错误

filelists = os.listdir("Path")
在这种情况下,变量
filelist
将是一个列表,比如
['a','B']
。创建数据帧时,需要使用
文件列表
而不是
[filelist]

df = pd.DataFrame(columns=filelists, index=wordlist)
df = df.append({'A':1, 'B':1}, ignore_index=True)

在第一个代码段中,您将对wordlist:中的单词使用
,但在第二个代码段中,您将使用
if文件内容中的功能:
。哪一个是正确的?对不起,第一个…我忘记编辑了…谢谢!是的,这肯定是第一个错误:)。但现在我得到了很多NaN,只有少数0和1。。。