2024-09-24 22:21:10 发布
网友
我有一些短信:
228;u;Ali; 129;cr;Daan; 730;c;Arton; 466;cr;Frynk; 314;c;Katuhkay; 9822;c;Kinberley;
我想把这个文本写到文件中,但我只想写带有符号“;cr;”的行
with open("input.csv", "r") as inp, open("output","w") as out: inpList = inp.read().split() out.write('\n'.join(el for el in inpList if ';cr;' in el))
如果要从web上读取数据,请使用以下方法:
read()一次读取整个文件。split()将其拆分为一个由空格分隔的列表。在
read()
split()
read(...) read([size]) -> read at most size bytes, returned as a string. If the size argument is negative or omitted, read until EOF is reached.
为了写入文件,'\n'.join([elem1,...])从包含“;cr;”的所有inpList元素创建一个字符串。此字符串被传递到write(str),后者将字符串打印到输出文件。在
'\n'.join([elem1,...])
write(str)
像这样:
with open("input.txt") as f,open("output.txt","w") as f2: for line in f: #iterate over each line of input.txt if ";cr;" in line: #if ';cr;' is found f2.write(line+'\n') #then write that line to "output.txt"
在python中,可以使用in轻松检查子字符串:
in
如果要从web上读取数据,请使用以下方法:
^{pr2}$read()
一次读取整个文件。split()
将其拆分为一个由空格分隔的列表。在为了写入文件,
'\n'.join([elem1,...])
从包含“;cr;”的所有inpList元素创建一个字符串。此字符串被传递到write(str)
,后者将字符串打印到输出文件。在像这样:
在python中,可以使用
^{pr2}$in
轻松检查子字符串:相关问题 更多 >
编程相关推荐