简体   繁体   English

适用于初学者的Python文件处理

[英]File handling in Python for beginners

Hello I am new to python and I am encountering this error : 您好,我是python新手,遇到此错误:

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

File "file_handling.py", line 4 文件“ file_handling.py”,第4行

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

CS\\test.txt,twoDarray,delimeter='\\t') ^ SyntaxError: unexpected character after line continuation character CS \\ test.txt,twoDarray,delimeter ='\\ t')^ SyntaxError:行继续符后出现意外字符

my code is this : 我的代码是这样的:

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

can anyone help please ? 有人可以帮忙吗?

Please use the code-syntax of stackoverflow so we can read your code easier. 请使用stackoverflow的代码语法,以便我们更轻松地阅读您的代码。

It seems like you spelled delimiter wrong. 似乎您将delimiter拼写错误。

Hi and welcome to StackOverflow. 嗨,欢迎来到StackOverflow。 Please use the tools StackOverflow provides to properly structure your post (eg mark code etc.) and make sure the indentation and newlines of the Python code is correct since it's part of the syntax. 请使用StackOverflow提供的工具来正确构造您的帖子(例如标记代码等),并确保Python代码的缩进和换行是正确的,因为这是语法的一部分。

Regarding the question it's probably an issue with your path which is not marked as string (must be enclosed in quotation marks) and contains backslashes, which are special escape characters in Python. 关于这个问题,可能是您的路径存在问题,该路径未标记为字符串(必须用引号引起来)并且包含反斜杠,这是Python中的特殊转义字符。 Depending on your operating system (Mac OS, Windows, Linux etc.) you might need to use forward slashes or double(!) backward slashes. 根据您的操作系统(Mac OS,Windows,Linux等),可能需要使用正斜杠或双(!)斜杠。

Try this: 尝试这个:

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

您的文件名应为字符串。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM