SyntaxError: (unicode error) 'unicodeescape' 编解码器无法解码位置 12-13 截断 \UXXXXXXXX 转义的字节

Posted

技术标签:

【中文标题】SyntaxError: (unicode error) \'unicodeescape\' 编解码器无法解码位置 12-13 截断 \\UXXXXXXXX 转义的字节【英文标题】:SyntaxError: (unicode error) 'unicodeescape' codes can't decode bytes in position 12-13 truncated \UXXXXXXXX escapeSyntaxError: (unicode error) 'unicodeescape' 编解码器无法解码位置 12-13 截断 \UXXXXXXXX 转义的字节 【发布时间】:2021-03-05 16:50:55 【问题描述】:

我尝试使用pd.read_csv("C:\Users\mfk15\Desktop\DM\tweets.xlsx"),但仍然出现错误。我用的是windows anaconda Jupyter notebook。

这是我现在面临的错误

File "<ipython-input-4-200f7da6a93c>", line 1
data_imdb = pd.read_csv("‪C:\Users\mfk15\Desktop\DM\tweets.xlsx", delimiter='\t', header=None)
                        ^

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 12-13: truncated \UXXXXXXXXXX escap

OSError 的额外回溯:

OSError                                   Traceback (most recent call last)
<ipython-input-25-c18122f86667> in <module>
----> 1 data_imdb=pandas.read_csv("‪C:/Users/mfk15/anaconda3/tweets.csv", delimiter='\t', header=None)
      2 data_imdb.columns = ["tweetText", "tweetCreated","Location"]
      3 
      4 data_imdb

~\anaconda3\lib\site-packages\pandas\io\parsers.py in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, dialect, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision)
    684     )
    685 
--> 686     return _read(filepath_or_buffer, kwds)
    687 
    688 

~\anaconda3\lib\site-packages\pandas\io\parsers.py in _read(filepath_or_buffer, kwds)
    450 
    451     # Create the parser.
--> 452     parser = TextFileReader(fp_or_buf, **kwds)
    453 
    454     if chunksize or iterator:

~\anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, f, engine, **kwds)
    944             self.options["has_index_names"] = kwds["has_index_names"]
    945 
--> 946         self._make_engine(self.engine)
    947 
    948     def close(self):

~\anaconda3\lib\site-packages\pandas\io\parsers.py in _make_engine(self, engine)
   1176     def _make_engine(self, engine="c"):
   1177         if engine == "c":
-> 1178             self._engine = CParserWrapper(self.f, **self.options)
   1179         else:
   1180             if engine == "python":

~\anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, src, **kwds)
   2006         kwds["usecols"] = self.usecols
   2007 
-> 2008         self._reader = parsers.TextReader(src, **kwds)
   2009         self.unnamed_cols = self._reader.unnamed_cols
   2010 

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader.__cinit__()

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._setup_parser_source()

OSError: [Errno 22] Invalid argument: '\u202aC:/Users/mfk15/anaconda3/tweets.csv'

【问题讨论】:

反斜杠在 Python 中被解释为转义字符。改用正斜杠或在文件名字符串前面加上 r 这些方法我都试过了,都没有解决 什么意思?我很确定这应该可行:pd.read_csv("‪C:/Users/mfk15/Desktop/DM/tweets.xlsx",...) 不,它显示另一个错误,称为“OSError”无效参数 嗯,这是一个不同的错误。这确实解决了SyntaxError 问题,不是吗?您能否提供OSError 的完整回溯。 【参考方案1】:

试试这个:

pd.read_csv(r"C:\Users\mfk15\Desktop\DM\tweets.xlsx")

【讨论】:

【参考方案2】:

只需用正斜杠替换所有反斜杠

pd.read_csv("‪C:/Users/mfk15/Desktop/DM/tweets.xlsx", delimiter='\t', header=None)

【讨论】:

以上是关于SyntaxError: (unicode error) 'unicodeescape' 编解码器无法解码位置 12-13 截断 \UXXXXXXXX 转义的字节的主要内容,如果未能解决你的问题,请参考以下文章

Uncaught SyntaxError: Invalid Unicode escape sequence异常处理

SyntaxError: (unicode error) 'unicodeescape' 编解码器无法解码位置 12-13 截断 \UXXXXXXXX 转义的字节

Python_报错:SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in posit

Snowflake 过程 JavaScript 编译错误:Uncaught SyntaxError: Unexpected token ':' in ERR_LOGGING at

SyntaxError :( unicode错误)'unicodeescape'编解码器无法解码位置2-3的字节:截断 UXXXXXXXX转义[重复]

Python中读取txt文本出现:SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes i