0

我有一个文本文档,我正在尝试查找退货并用逗号替换它们。我不断收到语法错误,最后的打印功能被突出显示,但我不知道出了什么问题。

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)
4

2 回答 2

6

字符串在 python 中是不可变的,因此您必须将该操作的返回值分配replace给某个变量:

order = each_line.replace("\n", ",").strip()  #use '\n' not '/n'  

这条线缺少一个 close ),这就是你得到那个的原因SyntaxError

except pickle.PickleError as perr:
    print('Pickling error: ' + str(perr))
                                        ^ 
                                        |
                                     missing
于 2013-06-26T17:35:59.297 回答
1

正如我在上面的评论中所写,您应该使用“\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)
于 2013-06-26T17:36:27.300 回答