简体   繁体   English

为什么字符串不会拆分为使用 python 从 csv 读取

[英]Why the string won't split as read from csv using python

Input config_note.csv:输入 config_note.csv:

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

I then only extract the last column by following command:然后我只通过以下命令提取最后一列:

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]))

But the output remains as:但是 output 仍然是:

Time: \nJoin Connection: \nUser Remark:

I've tried to input variable directly and get what I want:我试图直接输入变量并得到我想要的:

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

This is what I want:这就是我要的:

Time:
Join Connection: 
User Remark:

Could anyone please help me with the input issue from csv?谁能帮我解决 csv 的输入问题?

The problem is the string is encoded as a raw string, you can print it out into a normal string with问题是字符串被编码为原始字符串,您可以将其打印成普通字符串

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

so you can create a function:所以你可以创建一个 function:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM