【问题标题】:Why the string won't split as read from csv using python为什么字符串不会拆分为使用 python 从 csv 读取
【发布时间】:2020-07-29 03:44:16
【问题描述】:

输入 config_note.csv:

Default,case 1,case 2,Time: \nJoin Connection: \nUser Remark:

然后我只通过以下命令提取最后一列:

col_list = ["project", "name 1", "name 2", "note"]
df = pd.read_csv("config_note.csv", usecols=col_list, engine='python',error_bad_lines=False)
print('df["note"]:', str(df["note"].iloc[0]))

但输出仍然为:

Time: \nJoin Connection: \nUser Remark:

我试过直接输入变量,得到我想要的:

 test = "Time: \nJoin Connection: \nUser Remark:"
 print(test)

这就是我想要的:

Time:
Join Connection: 
User Remark:

谁能帮我解决 csv 的输入问题?

【问题讨论】:

  • 你想通过打印字符串来达到什么目的?

标签: python string csv split


【解决方案1】:

问题是字符串被编码为原始字符串,您可以将其打印成普通字符串

print(df["note"].iloc[0].encode().decode('unicode-escape'))

所以你可以创建一个函数:

def decode(x):
    return x.encode().decode('unicode-escape')
print(decode(df["note"].iloc[0]))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-29
    • 2014-03-08
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多