连接包含数字的多列文本文件(&N)

2024-10-01 07:50:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为17307的文件夹,其中包含一些名为

SEPT307A.17_.ismr, 
SEPT307B.17_.ismr, 
SEPT307C.17_.ismr,.... upto SEPT307X.17_.ismr. 

我想使用Python将所有这些连接到一个文本文件中。我试过:

st = 'path/to/folder'
a = input('Enter first part of file') #i.e. SEPT307 in file name
alph = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X']
yr = input('enter the year')
last = '_.ismr'
for letter in alph:
    st1 = st + "a" + alph + "." + "yr" + last
    fp = open(st1, "r")
    data=np.append(data, np.fromfile(fp, dtype=list))

也就是说,我尝试将所有内容放入数据中,然后将数据复制到单独的文本文件中。 然而,我得到了这个错误:

TypeError: Can't convert 'list' object to str implicitly

有人能提出一些方法吗


Tags: toininputdatanplistfilelast
1条回答
网友
1楼 · 发布于 2024-10-01 07:50:08

看起来错误来自此行:

st1 = st + "a" + alph + "." + "yr" + last

其中alph是字母表的完整列表。应该是:

st1 = st + "a" + letter + "." + "yr" + last

问题是您正在尝试将liststr连接起来

相关问题 更多 >