2016-09-29 54 views
0

你好,我是新来的蟒蛇,我遇到这个错误:文件在Python处理初学者

C:\Users\Dylan Galea\Desktop\Modelling and CS>python file_handling.py

File "file_handling.py", line 4

np.savetxt(\Users\Dylan Galea\Desktop\Modelling and

CS\test.txt,twoDarray,delimeter='\t') ^ SyntaxError: unexpected character after line continuation character

我的代码是这样的:

import numpy as np 

twoDarray =np.array([[1,2,3],[4,5,6]]) 
np.savetxt(\Users\Dylan Galea\Desktop\Modelling and CS\test.txt,twoDarray,delimeter='\t') 

谁能帮助吗?

+0

您需要使用引号将路径指定为字符串,即“Users/Dylan Galea/Desktop/Modeling and CS/test.txt”。请注意,'\'需要像'\\'一样转义,如果你的路径以'\'或'/'开头,它应该是绝对的。 –

回答

0

请使用stackoverflow的代码语法,以便我们更容易阅读代码。

好像你拼错delimiter错了。

0

嗨,欢迎来到StackOverflow。请使用StackOverflow提供的工具来正确地构建您的文章(例如标记代码等),并确保Python代码的缩进和换行符正确,因为它是语法的一部分。

关于这个问题,它可能是一个问题,你的路径没有标记为字符串(必须用引号引起来)并包含反斜杠,它们是Python中的特殊转义字符。根据您的操作系统(Mac OS,Windows,Linux等),您可能需要使用正斜杠或双斜杠(!)。

试试这个:

twoDarray = np.array([[1,2,3],[4,5,6]]) 
np.savetxt("/Users/Dylan Galea/Desktop/Modelling and CS/test.txt", twoDarray,delimeter='\t') 
0

你的文件名应该是一个字符串。

np.savetxt(r'\Users\Dylan Galea\Desktop\Modelling and CS\test.txt',twoDarray,delimeter='\t')