【发布时间】:2013-10-11 05:04:28
【问题描述】:
我是 Python 新手,正在从教程中学习,这里有这一行假设从 .csv 文件中读取并跳过第一行(标题):
data = open("banklist.csv", "r").readlines()[1:]
for entry in data:
#do some stuffs
问题是,它甚至没有进入循环。删除 open 语句中的 [1:] 并添加打印数据,我发现 csv 似乎被读取为一大行,而删除标题则删除了所有内容:
["First National Bank also operating as The National Bank of El Paso,Edinburg,TX,14318,13-Sep-13,18-Sep-13\rThe Community's Bank,Bridgeport,CT,57041,13-Sep-13,17-Sep-13\rSunrise Bank of Arizona,Phoenix,AZ,34707,23-Aug-13,11-Sep-13\rCommunity South Bank,Parsons,TN,19849,23-Aug-13,5-Sep-13\rBank of Wausau,Wausau,WI,35016,9-Aug-13,4-Sep-13\rFirst Community Bank of Southwest Florida (also operating as Community Bank of Cape Coral),Fort Myers,FL,34943,2-Aug-13,26-Aug-13\rMountain National Bank,Sevierville,TN,34789,7-Jun-13,26-Aug-13\r1st Commerce Bank,North Las Vegas,NV,58358,6-Jun-13,26-Aug-13\rBanks of Wisconsin d/b/a Bank of Kenosha,Kenosha,WI,35386,31-May-13,12-Jul-13\rCentral Arizona Bank,Scottsdale,AZ,34527,14-May-13,26-Aug-13\rSunrise Bank,Valdosta,GA,58185,10-May-13,12-Jul-13\rPisgah Community Bank,Asheville,NC,58701,10-May-13,26-Aug-13\rDouglas County Bank,Douglasville,GA,21649,26-Apr-13,26-Aug-13\rParkway Bank,Lenoir,NC,57158,26-Apr-13,26-Aug-13\rChipola Community Bank,Marianna,FL,58034,19-Apr-13,12-Jul-13\rHeritage Bank of North Florida,Orange Park,FL,26680,19-Apr-13,26-Aug-13\rFirst Federal Bank,Lexington,KY,29594,19-Apr-13,12-Jul-13"]
我该怎么做?
【问题讨论】:
-
Python 有一个 csv 解析器模块
-
我可以使用那个模块,但知道为什么会这样吗?
-
可能是换行符转义序列不匹配?
-
回车符
\r未正确处理,因此出现错误,请使用csv模块 -
我必须说我不太明白为什么这个问题被否决了; OP 展示了他的尝试,预期的输出,以及他得到的结果。 '使用
csv模块' 的幼稚评论实际上也不能解决问题。