导入字符串以从.csv文件

时间:2017-10-17 01:15:33

标签: python-2.7 pandas

我指的是我在几天前发布的this问题,我还没有得到任何回复,我怀疑情况没有得到恰当的描述,我做了一个更简单的设置,更容易理解,并希望。得到有经验的程序员的更多关注!

我忘了提及,我在Jupyter上运行Python 2

import pandas as pd
from pandas import Series, DataFrame

g_input_df = pd.read_csv('SetsLoc.csv')
URL=g_input_df.iloc[0,0]
c_input_df = pd.read_csv(URL)
c_input_df = c_input_df.set_index("Parameter")

root_path = c_input_df.loc["root_1"]
input_rel_path = c_input_df.loc["root_2"]
input_file_name = c_input_df.loc["file_name"]

本节从.csv中读取一个路径列表,一次只读取一个路径,每个路径都指向另一个.csv文件,该文件包含使用python设置模拟的输入。

上述代码的结果可以在这里测试:

c_input_df
Value Parameter 
root_1  C:/SimpleTest/
root_2  Input/
file_name   Prop_1.csv

URL
'C:/SimpleTest/Sets/Set_1.csv'

root_path+input_rel_path+input_file_name
Value    C:/SimpleTest/Input/Prop_1.csv
dtype: object

Property_1 = pd.read_csv('C:/SimpleTest/Input/Prop_1.csv')
Property_1
height  weight
0   100 50
1   110 44
2   98  42

...另一方面,如果我尝试使用varibales来描述文件的路径和名称,我会收到错误:

Property_1 = pd.read_csv(root_path+input_rel_path+input_file_name)
Property_1

我收到以下错误:

ValueErrorTraceback (most recent call last)
<ipython-input-3-1d5306b6bdb5> in <module>()
----> 1 Property_1 = pd.read_csv(root_path+input_rel_path+input_file_name)
      2 Property_1

C:\ProgramData\Anaconda2\lib\site-packages\pandas\io\parsers.pyc in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, skipfooter, skip_footer, doublequote, delim_whitespace, as_recarray, compact_ints, use_unsigned, low_memory, buffer_lines, memory_map, float_precision)
    653                     skip_blank_lines=skip_blank_lines)
    654 
--> 655         return _read(filepath_or_buffer, kwds)
    656 
    657     parser_f.__name__ = name

C:\ProgramData\Anaconda2\lib\site-packages\pandas\io\parsers.pyc in _read(filepath_or_buffer, kwds)
    390     compression = _infer_compression(filepath_or_buffer, compression)
    391     filepath_or_buffer, _, compression = get_filepath_or_buffer(
--> 392         filepath_or_buffer, encoding, compression)
    393     kwds['compression'] = compression
    394 

C:\ProgramData\Anaconda2\lib\site-packages\pandas\io\common.pyc in get_filepath_or_buffer(filepath_or_buffer, encoding, compression)
    208     if not is_file_like(filepath_or_buffer):
    209         msg = "Invalid file path or buffer object type: {_type}"
--> 210         raise ValueError(msg.format(_type=type(filepath_or_buffer)))
    211 
    212     return filepath_or_buffer, None, compression

ValueError: Invalid file path or buffer object type: <class 'pandas.core.series.Series'>}

我相信问题在于从数据框中读取构成路径和filenemae的参数的方式,有没有办法指定那些参数是路径,还是类似的东西可以避免这个问题?

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我在与此帖相关的其他问题中发布了解决方案,以防有人想看看:

Problems opening a path in Python