我假设这里的每个人都熟悉所有文本文件应以换行符结尾的格言.多年来我一直都知道这个"规则",但我一直在想 - 为什么?
Python的泡菜(我说的是标准的Python 2.5/2.6/2.7)不能腌制锁,文件对象等.
它也不能pickle生成器和lambda表达式(或任何其他匿名代码),因为pickle实际上只存储名称引用.
在锁和依赖于操作系统的功能的情况下,原因为何你不能咸菜他们是明显的,是有道理的.
但为什么你不能发泡?
注:只是为了清楚起见, -我有兴趣的根本原因(或假设和进入该设计决策选择)为什么,而不是"因为它给你一个味酸错误".
我意识到这个问题有点广泛的目标,所以这里有一个经验法则,你是否回答:"如果这些假设被提出,或者允许的发电机的类型在某种程度上受到限制,那么酸洗发电机会再次工作吗?"
假设我有列表得分= [1,2,3,4,5],并且在我的程序运行时它会被更改.如何将其保存到文件中以便下次运行程序时我可以将更改后的列表作为列表类型进行访问?
我试过了:
score=[1,2,3,4,5]
with open("file.txt", 'w') as f:
for s in score:
f.write(str(s) + '\n')
with open("file.txt", 'r') as f:
score = [line.rstrip('\n') for line in f]
print(score)
Run Code Online (Sandbox Code Playgroud)
但这会导致列表中的元素不是整数.
我有一个词典列表.有时,我想更改并保存其中一个词典,以便在重新启动脚本时使用新消息.现在,我通过修改脚本并重新运行来进行更改.我想把它从脚本中拉出来并将字典列表放到某种配置文件中.
我已经找到了如何将列表写入文件的答案,但这假设它是一个平面列表.我怎么能用词典列表呢?
我的列表看起来像这样:
logic_steps = [
{
'pattern': "asdfghjkl",
'message': "This is not possible"
},
{
'pattern': "anotherpatterntomatch",
'message': "The parameter provided application is invalid"
},
{
'pattern': "athirdpatterntomatch",
'message': "Expected value for debugging"
},
]
Run Code Online (Sandbox Code Playgroud) 在python中,我有一个函数返回网站上最新链接(到文件夹)的列表.我还有另一个从这些文件夹下载最新文件的功能.我计划每天运行这个脚本.我有一个全局列表,其中包含下载功能每次运行最新文件夹时访问的文件夹链接.我想每五天更新一次全局列表,并在接下来的5天内保持静态,我会运行代码,直到它再次更新.
它有点像这样:
list = ["link1", "link2",...]
def update():
#code to update list
return list
def download(list):
#code to download from links
Run Code Online (Sandbox Code Playgroud)
所以我希望更新功能每5天运行一次(我知道该怎么做)以及每天运行的下载功能.那么如何将update()static返回的列表作为全局列表保留,直到它再次更新为止?
编辑:让我试着澄清一下:
我在星期一运行这个:
list = ["link1", "link2"]
def update():
#code to update list
return list #--> list = ["link1", "link2", "link3"]
def download(list):
#code to download from links
Run Code Online (Sandbox Code Playgroud)
这工作正常,列表已更新并在download()中使用.
我在星期二运行:
list = ["link1", "link2"]
#update() won't run today, only runs every 5 days
def update():
#code to update list
return list #--> list = ["link1", "link2", "link3"]
def download(list): …Run Code Online (Sandbox Code Playgroud) 阅读this question和this question后,我试图将列表(mylist)的每个元素写在文本文件(text.txt)的换行符上。
因此,例如,列表
mylist = ['a', 'b', 'ccc', 'dd', 'eeee', 'f', 'ggg']
Run Code Online (Sandbox Code Playgroud)
应当在书面text.txt像这样
a
b
ccc
dd
eeee
f
ggg
Run Code Online (Sandbox Code Playgroud)
我试过这个:
filename = 'text.txt'
with open(filename, mode="wb") as outfile: # also, tried mode="rb"
for s in mylist:
outfile.write("%s\n" % s)
Run Code Online (Sandbox Code Playgroud)
它创建了文本文件,但随后给出了错误;要么 aTypeError: a bytes-like object is required, not 'str'要么io.UnsupportedOperation: write取决于mode我使用的。
任何想法,以及对我做错了什么的简短解释,将不胜感激。
>>> w
['Parts-of-speech', 'disambiguation', 'techniques', '(', 'taggers', ')',
'are', 'often', 'used', 'to', 'eliminate', '(', 'or', 'substantially',
'reduce', ')', 'the', 'parts-of-speech', 'ambiguitiy', 'prior', 'to',
'parsing.', 'The', 'taggers', 'are', 'all', 'local', 'in', 'the', 'sense',
'that', 'they', 'use', 'information', 'from', 'a', 'limited', 'context',
'in', 'deciding', 'which', 'tag', '(', 's', ')', 'to', 'choose', 'for',
'each', 'word.', 'As', 'is', 'well', 'known', ',', 'these', 'taggers',
'are', 'quite', 'successful', '.']
>>> q=open("D:\unieng.txt","w")
>>> q.write(w)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: argument …Run Code Online (Sandbox Code Playgroud) python中是否有一个函数允许我们将列表保存在txt文件中并保留其格式?
如果我有这个清单:
values = ['1','2','3']
Run Code Online (Sandbox Code Playgroud)
我可以将其保存到包含以下内容的文件中:
'['1','2','3']'
Run Code Online (Sandbox Code Playgroud)
到目前为止,我在终端中打印部分列表并将其复制到txt文件中.
我想使用Python将两个变量写入文件.
根据这篇文章中的内容,我写道:
f.open('out','w')
f.write("%s %s\n" %str(int("0xFF",16)) %str(int("0xAA",16))
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
Traceback (most recent call last):
File "process-python", line 8, in <module>
o.write("%s %s\n" %str(int("0xFF", 16)) %str(int("0xAA", 16)))
TypeError: not enough arguments for format string
Run Code Online (Sandbox Code Playgroud) 我想知道如何保存用户输入的列表。我想知道如何将其保存到文件中。当我运行程序时,它说我必须使用一个字符串来写入它。那么,有没有办法将列表分配给文件,或者更好的是每次程序运行时它都会自动更新文件上的列表?如果文件最好是 .txt,那就太好了。
stuffToDo = "Stuff To Do.txt"
WRITE = "a"
dayToDaylist = []
show = input("would you like to view the list yes or no")
if show == "yes":
print(dayToDaylist)
add = input("would you like to add anything to the list yes or no")
if add == "yes":
amount=int(input("how much stuff would you like to add"))
for number in range (amount):
stuff=input("what item would you like to add 1 at a time")
dayToDaylist.append(stuff)
remove = input("would you like to remove …Run Code Online (Sandbox Code Playgroud) 我有一个列表,我想将该列表写入文件txt
lines=[3,5,6]
result = open("result.txt", "w")
result.writelines(lines)
result.close()
Run Code Online (Sandbox Code Playgroud)
但是当我跑步时,我收到以下错误:
writelines()参数必须是一个字符串序列
python ×11
list ×5
file ×3
pickle ×2
python-3.x ×2
string ×2
dictionary ×1
file-io ×1
function ×1
generator ×1
global ×1
newline ×1
python-2.7 ×1
text-files ×1
unix ×1
writefile ×1