【问题标题】:how to read a csv file that has multiple lines within the same cell?如何读取在同一单元格中有多行的 csv 文件?
【发布时间】:2021-03-07 06:32:38
【问题描述】:

我提前为图片道歉 - 我不知道如何在 stackoverflow 的单个单元格中表示多行。

我有一个 csv 文件,它在单个单元格中包含多行,如表 1 所示。我需要每个单元格中的每一行都有自己的列。幸运的是,每列每个单元格的行数相同。 IE。 column-1 每行 4 行,column-2 每行 2 行,依此类推...

我希望结果看起来像表 2,其中每行都有一列。我一直在尝试使用 splitlines() 来做到这一点,但没有成功。

表 2

c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11 c12 c13 c14 c15
City of Pflugerville New Residential Permits 1-1-2010 12:00:00 AM thru 3-6-2021 12:00:00 AM City 13-23598 6-11-2013 120000 AM Residential Residential Building 1901 Meandering Meadows Dr Falcon Point Sec 4 South Ryland Homes 1101 Arrow Point Dr. Ste. # 101 Cedar Park TX 78613 208200 Living - 2914 - Total - 3786.00 Permits Issued in the City: 4573
City of Pflugerville New Residential Permits 1-1-2010 12:00:00 AM thru 3-6-2021 12:00:00 AM City 13-23599 6-11-2013 000 Residential Residential Building 3309 Pasqueflower Pass Falcon Pointe Sec 12 Ryland Homes 1101 Arrow Point Dr Cedar Park TX 78613 267100 Living - 3853 - Total - 4856.00 Permits Issued in the City: 4573

如果您能指出正确的方向,我将不胜感激。

【问题讨论】:

  • 您可以将原始 csv 文件发布到前 n 行吗?

标签: python pandas dataframe


【解决方案1】:

我相信这应该可行。 pandas 也可以很好地处理多行文本,因此请尝试使用 pd.read_csv()。

import csv

file_path = 'yourfile.csv'

with open(file_path, newline='', encoding='utf-8') as f:
    reader = csv.reader(f, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    for row in reader:
        print(row)

【讨论】:

  • 太好了! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-10
  • 1970-01-01
  • 2015-06-15
  • 2021-11-19
  • 1970-01-01
  • 2023-01-18
相关资源
最近更新 更多