我想将文件循环到一个目录中,在这些文件上创建一些东西,然后为每个文件写出结果。 但我的文件无法读取,因为python将文件名解释为字符串对象而不是可读文件。 有没有办法避免这种情况?
import re
import os
def create_filename_for_fileout (f1):
fileout_n = f1.replace("TT", "out")
fileout = "C:\\Users\\KP\\Desktop\\FSC_Treetag\\out\\"+str(fileout_n)
return fileout
for file_in in os.listdir('C:\\Users\\KP\\Desktop\\FSC_Treetag'):
filename = str(file_in)
file_out = create_filename_for_fileout(filename)
open(file_in, 'r')
open(file_out, 'w')
content_file = file_in.readlines()
for ln in content_file:
regex = re.compile('(.*\t(ADJ|ADV|NOM|VER:cond|VER:futu|VER:impe|VER:impf|VER:infi|VER:pper|VER:pres|VER:pres|VER:simp|VER:subi|VER:subp)\t(.*))')
res = regex.search(ln)
if res:
# categ = res.group(2)
lemme = res.group(3)
file_out.write(str(lemme)+"\n")
file_out.close()
file_in.close()
结果:
content_file = file_in.readlines()
AttributeError: 'str' object has no attribute 'readlines'
>>>
答案 0 :(得分:2)
您未将 $json = '{"request_list":[{"id":"1","name":"yunus","surname":"smsk","phone_number":"05350601922","blood_type":"0","unit_of_blood":"0","date":null},{"id":"3","name":"yunus","surname":"smsk","phone_number":"05350601922","blood_type":"0","unit_of_blood":"0","date":null}]}';
$data = json_decode($json );
$request = $data->request_list;
foreach($request as $request_data){
echo $request_data->id;
echo $request_data->name;
echo $request_data->surname;
}
分配给任何要使用的变量。
open
答案 1 :(得分:1)
您没有将Bitmasks = []
Bitmasks.append(int("0001", 2))
Bitmasks.append(int("0010", 2))
Bitmasks.append(int("0100", 2))
Bitmasks.append(int("1000", 2))
myNumber = 12
Bools = []
for bm in Bitmasks:
Bools.append(myNumber|bm==myNumber)
函数分配给相应的open
(handlers
正在返回文件类型的对象。)
open
应该是:
filename = str(file_in)
file_out = create_filename_for_fileout(filename)
open(file_in, 'r')
open(file_out, 'w')
注意:为了清楚起见,最好为 infile file_out = open(create_filename_for_fileout(file_in), 'w')
file_in = open(file_in, 'r')
使用另一个指针。
检查:https://docs.python.org/2/library/functions.html#open
open(name [,mode [,buffering]])
handler