我有一个文本文档,我试图找到返回并用逗号替换它们。我一直收到语法错误,最后的打印功能突出显示,但我不知道出了什么问题。
orders = []
try:
with open('vlc.txt') as data:
for each_line in data:
try:
each_line.replace("/n", ",")
order = order.strip()
orders.append(order)
except ValueError:
pass
with open("vlc_clean.txt","wb") as vlc_file:
pickle.dump(orders, vlc_file)
except IOError as err:
print('File Error: ' + str(err))
except pickle.PickleError as perr:
print('Pickling error: ' + str(perr)
print(orders)
答案 0 :(得分:5)
字符串在python中是不可变的,因此您必须将该replace
操作的返回值赋给某个变量:
order = each_line.replace("\n", ",").strip() #use '\n' not '/n'
此行缺少结束)
,这就是您获得SyntaxError
的原因:
except pickle.PickleError as perr:
print('Pickling error: ' + str(perr))
^
|
missing
答案 1 :(得分:0)
正如我在上面的评论中所写,您应该使用“\ n”作为换行符,并注意您的变量。
这是工作代码(我忽略了保存作为泡菜):
orders = []
try:
with open('vlc.txt') as data:
for each_line in data:
try:
order = each_line.replace("\n", ",").strip()
orders.append(order)
except ValueError:
pass
except:
pass
print(orders)