我需要询问字符串之间的区别\r\n,\r和\n.字符串如何受每个字符串的影响?
我不得不更换的出现\r\n,并\r用\n,但我不能让他们如何在一个字符串不同...
我知道这\r就像打击进入并且 \n是一条新线.
我想使用 Python 3 读取每行由换行符 ('\n') 指示的 csv 文件。这是我的代码:
import csv
with open(input_data.csv, newline ='\n') as f:
csvread = csv.reader(f)
batch_data = [line for line in csvread]
Run Code Online (Sandbox Code Playgroud)
上面的代码给出了错误:
batch_data = [line for line in csvread].
_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?
Run Code Online (Sandbox Code Playgroud)
阅读这些帖子:CSV new-line character seen in unquoted field error,也尝试了我能想到的这些替代方案:
with open(input_data.csv, 'rU', newline ='\n') as f:
csvread = csv.reader(f)
batch_data = [line for line in csvread]
with …Run Code Online (Sandbox Code Playgroud) 我正在准备一个cvs文件,
这是我的代码:
import csv
class CsvToJson:
def __init__(self, csvFilePath):
with open(csvFilePath, 'rb') as csvFile:
spamreader = csv.reader(csvFile, delimiter= ‘;’, quotechar = '|')
for row in spamreader:
print ', '.join(row)
k = CsvToJson(csvFilePath = 'carsModelsMakes.csv')
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误
SyntaxError: Non-ASCII character '\xe2' in file CsvToJson.py on line 7, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
Run Code Online (Sandbox Code Playgroud)
在第五行.
我在互联网上阅读,似乎解决方案是使用
# -*- coding: utf-8 -*-
Run Code Online (Sandbox Code Playgroud)
在文件的开头.
我做到了,但后来我收到了这个错误:
File "CsvToJson.py", line 6
spamreader = csv.reader(csvFile, delimiter= ‘;’, quotechar = '|')
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
你能帮忙吗?