尝试从文件hello.txt
的路径C:/temp/Kälte/Grad°/
中读取内容:
消息;内容
问候;世界,您好!
path = 'C:/temp/Kälte/Grad°/hello.txt'
# OKAY
data = ""
with open(path) as f:
data = f.read()
print("file content:\n" + data)
# OKAY
import csv
with open(path, 'r') as csvfile:
print("reading csv file:\n"+path)
reader = csv.reader(csvfile, delimiter=';')
for row in reader:
print(row)
# FAILURE
import pandas as pd
print("reading csv file with pandas:\n"+path)
dataframe = pd.read_csv(path, sep=';',names=["Message","Content"],header=1)
print (dataframe)
我还记得几年前完全一样的问题,我想知道为什么没人能解决熊猫这种基本问题。熊猫不是在内部调用python的open
吗(因为那样就可以了,不是吗)?还是我做错了?
答案 0 :(得分:1)
这似乎是this issue的重复出现,但是那里的解决方案对我的设置没有帮助。
如果没有其他选择,这是一种手动解决方案:
path = r'C:/temp/Kälte/Grad°/hello.txt'
import pandas as pd
import csv
with open(path, 'r') as fp:
reader = csv.reader(fp, delimiter=';')
cols = next(reader)
df = pd.DataFrame(list(reader), columns=cols)
print(df)
Message Content
0 Greeting Hello World!