You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

提取文件第2、3、4列值生成单列列表时遇SyntaxError错误求助

问题排查与修复

你的脚本存在几个明确错误,直接导致了报错,逐一说明并修复:

  • 变量名拼写错误:循环内你写了colunms=line.split(),这里少了字母m,正确写法是columns=line.split()。后续代码调用的是columns[1]等,前面的拼写错误会导致columns变量未定义,解释器可能会把报错位置偏移到后续行,出现误导性的SyntaxError提示。

  • 函数名不匹配:你定义的函数是extract_values,但调用时写的是extract_and_write_values,名称不一致会触发NameError。

  • 额外兼容提示:如果你的Python版本低于3.6,f-string语法(f"{value}")不被支持,也会引发SyntaxError。这种情况要么升级Python到3.6及以上,要么改用str.format()写法,比如"{}\n{}\n{}\n".format(value2, value3, value4)。


修正后的完整脚本

def extract_values(input_file_path, output_file_path):
    """Extract values from the second, third, and fourth columns of a file."""
    with open(input_file_path, 'r') as input_file:
        lines = input_file.readlines()[2:]
        values_column2 = []
        values_column3 = []
        values_column4 = []
        for line in lines:
            columns = line.split()  # 修正拼写错误
            values_column2.append(columns[1])
            values_column3.append(columns[2])
            values_column4.append(columns[3])

    with open(output_file_path, 'w') as output_file:
        for value2, value3, value4 in zip(values_column2, values_column3, values_column4):
            output_file.write(f"{value2}\n{value3}\n{value4}\n")

input_file_path = "file3.xyz"
output_file_path = "v1.txt"
extract_values(input_file_path, output_file_path)  # 修正函数调用名
print(f"Values have been extracted and written to {output_file_path}.")

内容的提问来源于stack exchange,提问作者sumsum

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 00:03:09