【问题标题】:read a column containing a comma as single string in csv [duplicate]在csv中读取包含逗号作为单个字符串的列[重复]
【发布时间】:2020-03-20 09:38:30
【问题描述】:

我正在尝试使用 scv.reader 在我的 python 代码中写入一个 csv 文件。它按预期工作正常,但是当涉及到包含逗号的列时,它会将单列转换为多列。我的简历如下所示。

1,Most Startups fail*,"Yes, most startups fail.","No, most startups succeed and provide big return to investors.",,,,,

现在我想要“是的,大多数初创公司都失败了。”成为单列,但它将其转换为包含“是”和“大多数初创公司失败。”的两列,但我想要一个包含“是的,大多数初创公司失败。”的单元格。我怎样才能做到这一点?任何帮助/建议表示赞赏。

【问题讨论】:

    标签: python python-3.x csv


    【解决方案1】:

    使用下面的代码,或许对你有帮助:

    import re
    import csv
    
    columnValue = input()
    ouput = re.split('.""*', columnValue)  # doing split here
    
    for i in ouput:
        print('\n', i)           # To show the output of text splited 
    
    newfilePath = 'D:/Machine_Learning/General/ss.csv'  # the path location of CSV file to Write
    
    # full code for writting column name as wanted 
    with open(newfilePath, "w") as csvfile:
        writer = csv.writer(csvfile)    
        writer.writerow(ouput)
    csvfile.close()
    

    结果截图:

    使用适用于 csv 的文件路径位置运行此代码。

    【讨论】:

    • 请不要在您的答案(以及问题)中包含代码截图。
    猜你喜欢
    • 2023-03-05
    • 2011-01-11
    • 1970-01-01
    • 2013-07-29
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多