如何删除CSV文件中包含在DOUBLE Quotes Python pandas中的行

alen0pnh  于 2023-06-19  发布在  Python
关注(0)|答案(2)|浏览(146)

我是Python的新手,我在使用CSV文件时遇到了麻烦。我有一个CSV示例文件,包含如下数据:

"123,hahaha,,,"
"345,hohoho,,,"

当我使用read_csv时,输出将每一行视为第一列。我需要输出为:

123,hahaha,,,
345,hohoho,,,

每一行都应被视为一个单独的列。有人能帮我吗?

xmd2e60i

xmd2e60i1#

你可以试试这个:

df = (
    pd.read_csv("file.csv", header=None, quoting=3) # QUOTE_NONE
        .replace('"', "", regex=True).iloc[:, :-1]
         # .set_axis(["col1", ...]) # optional
)

输出:

print(df)

     0       1   2   3
0  123  hahaha NaN NaN
1  345  hohoho NaN NaN
iq0todco

iq0todco2#

首先重新格式化文件:使用下面的代码删除开始双引号、结束换行符和结束双引号:

# Assuming your csv file is named 'file.csv' and is in the same directory that this code is running
with open ("formatted_file.csv", "w") as formatted_file:
    with open ("file.csv", "r") as file:
        for line in file:
            formatted_line = line.rstrip()[1:-1] + "\n"
            formatted_file.write(formatted_line)

然后使用新创建的文件。要检查它是否正常,您可以阅读以下内容:

with open ("formatted_file.csv", "r") as formatted_file:
    for line in formatted_file:
        print(line, end='')

相关问题