我知道答案很简单,而且在某个地方,但我似乎找不到它。正如我的标题所暗示的那样,我正在尝试将具有.csv格式的动态文件名的熊猫DF写入给定路径的输出目录。下面是我的错误代码。感谢您的任何建议,如果可能的话,我愿意以不同的或pythonic的方式执行此操作。
runfile('C:/Users/U321103/.spyder-py3/Read_VORTEX_test_files.py', wdir='C:/Users/U321103/.spyder-
py3')
klondikeii.Vortex_WIND.csv
Traceback (most recent call last):
File "C:UsersU321103.spyder-py3Read_VORTEX_test_files.py", line 29, in <module>
c.to_csv(Path(p + filename ), index=False)
TypeError: unsupported operand type(s) for +: 'WindowsPath' and 'str'
我的代码如下所示:
from sys import exit
import pandas as pd
from pathlib import Path
# Create a dataframe from csv
df = pd.read_csv("\porfiler03\gtdshare\VORTEX\VALIDATION\vortex_links.txt", delimiter=',')
# User list comprehension to create a list of lists from Dataframe rows
list_of_rows = [list(row) for row in df.values]
# Print list of lists i.e. rows
#print(list_of_rows)
var = df.variable.to_frame() #extract wind or power from df 'variable' column
#Find farm_data = farm_vortex cases
#for k in range(0,len(df)):
for k in range(0,1):
if (( df.farm_data[k] == df.farm_vortex[k]) and var.variable[k] == 'wind'):
#print('chinook = vortex data')
c = pd.read_csv(df.link[k])#dataframe with vortex wind data
filename = df.farm_data[k] + '.' + 'Vortex_WIND' + '.csv'
print(filename)
#save the data to a csv_file.
p = Path('///porfiler03//gtdshare//')
c.to_csv(Path(p + filename ), index=False)
exit()
从 '///porfiler03//gtdshare//' 中删除 Path((。
在 p + 文件名中,您正在尝试连接以创建完整路径。您可以对两个字符串执行此操作,但不能对字符串和路径执行此操作,这就是错误告诉您的内容。 尝试:
p = '///porfiler03//gtdshare//'
c.to_csv(Path(p + filename ), index=False)