【问题标题】:Python - CSV read as one big linePython - CSV 读取为一大行
【发布时间】: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 模块' 的幼稚评论实际上也不能解决问题。

标签: python csv readlines


【解决方案1】:

您正在阅读的文本文件使用了与您的系统不同的换行符约定; \r 您的系统可能需要 \n\r\n

您可以使用通用换行支持打开文件,而不是使用'rU' 模式:

data = open("banklist.csv", "rU").readlines()[1:]

跳过第一行的更聪明的方法是将文件用作迭代器;直接循环它。 next() 方法让您一次抓取一行,为您提供跳过第一行的方法:

with open("banklist.csv", "rU") as infile:
    next(infile, None)  # skip the first line
    for line in infile:
        print line 

但是,如果这是 CSV 文件,请使用 csv 模块读取数据:

import csv

with open("banklist.csv", "rU") as infile:
    reader = csv.reader(infile)
    next(reader, None)  # skip the first row
    for row in reader:
        print row

CSV 模块会为您处理将行拆分为列表:

['First National Bank also operating as The National Bank of El Paso', 'Edinburg', 'TX', '14318', '13-Sep-13', '18-Sep-13']
["The Community's Bank", 'Bridgeport', 'CT', '57041', '13-Sep-13', '17-Sep-13']
['Sunrise Bank of Arizona', 'Phoenix', 'AZ', '34707', '23-Aug-13', '11-Sep-13']
['Community South Bank', 'Parsons', 'TN', '19849', '23-Aug-13', '5-Sep-13']
['Bank of Wausau', 'Wausau', 'WI', '35016', '9-Aug-13', '4-Sep-13']
['First Community Bank of Southwest Florida (also operating as Community Bank of Cape Coral)', 'Fort Myers', 'FL', '34943', '2-Aug-13', '26-Aug-13']
# etc.

【讨论】:

  • 谢谢!那个 \r 看起来真的很可疑。
猜你喜欢
  • 2018-12-08
  • 1970-01-01
  • 1970-01-01
  • 2015-05-13
  • 1970-01-01
  • 2016-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多