python - 发现打开文件错误 : TypeError: coercing to Unicode: need string or buffer, 列表

标签 python

我有以下代码:基本上我正在做的就是寻找一个 .csv 文件,该文件可能位于两个位置之一(或更多取决于) 我在文本文件 (LocationsFile.txt) 中有两个特定位置。

除此之外,我只想获取学生的特定字段:这是SSID 字段

我有以下代码,但它似乎给我的错误如下:

    Tape Name130322
['\\\\....HIDDEN FOR Confidenciality .....StudentDB1_av.csv']
Success:
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "C:\Users\Administrator\Desktop\John\Spreadsheet\Gui.py", line 291, in run
    findStudentData('130322','StudentDB1')
  File "C:\Users\Administrator\Desktop\John\Spreadsheet\Gui.py", line 62, in findStudentData
    with open(items) as f:
TypeError: coercing to Unicode: need string or buffer, list found

正在执行的代码如下: - 请在回复时考虑周到,因为我是一个“新手”Python程序员!

def findStudentData(student_name,course):
student_name= student_name.lower()
configfiles = []

print "student_name" + student_name
for path in MMinfo_paths:
    configfiles.append(glob.glob(path+"/" + course+"*"))

for items in configfiles:
    print items
    print "Success:"
    heading = True

    with open(items) as f:
        content = f.read()

        if heading == True
            ssidCol = content.index('ssid')
            flagsCol = content.index('flags')
            nameCol = content.index('name')
            heading = False
            continue



        for item in rowData:
            if rowData.index(item) == nameCol:
                print rowData
            else:
                continue

非常感谢:-)

最佳答案

现在,您的配置文件如下所示:

[[file1, file2], [file3, file4]]

您可以执行以下操作:

for items in configfiles:
    for filename in items:
        with open(items) as f: ...

或者,您可以将 configfiles.append 替换为 configfiles.extend。 Append 将 glob 返回的列表添加为配置文件列表的元素,而extend 将 glob 返回的列表的每个元素添加到配置文件列表。那么,配置文件将是:

[file1, file2, file3, file4]

你可以这样写:

for items in configfiles:
    with open(items) as f: ...

关于python - 发现打开文件错误 : TypeError: coercing to Unicode: need string or buffer, 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17176984/

相关文章:

python - 使用 Python 中的 Keras 和 TensorFlow 无法重现结果

python - 使用 python 使用 Active Directory 密码连接 Azure SQL Server 数据库(出现错误)

具有等号和空格的 Python 子进程参数

python - 预测自行车站的可用性

python - 从 python 3 中的 fetchone() 调用中检索值

python - 附加到 python/pandas 中的系列不起作用

python - 如何将 Python Pandas DataFrame 写入具有特定列类型格式的 .XLS 文件?

python - 导入错误 : No module named 'sha'

python - 如何将输出转换为json格式?

python - 在 python 中使用 if 语句有条件地增加整数计数