将文本文件中的单词替换为列表中的单词执行一些操作,然后重新开始



我是编程初学者,可能需要一些帮助。我有一个有30000个不同值的列表;猫;在具有列表中第一个值的文本文件中执行某些操作,在这些操作中,我使用文本文件,然后使用列表中的下一个值重新开始。文本文件看起来像这样:

    <QVariantList name="Filters">
        <QVariantMap>
            <QVariantMap name="Filter">
                <QUrl name="[OPENFILE]FactorImage" value=""/>
                <QUrl name="[OPENFILE]OffsetImage" value=""/>
                <bool name="enabled" value="true"/>
                <double name="factor" value="1"/>
                <QString name="name" value="DSNU"/>
                <int name="offset" value="cat"/>

我的清单是这样的:

[[-2271.0000000000005],
 [-2318.0],
 [-2377.0],
 [-2425.0],
 [-2406.0],
 [-2360.9999999999995],
 [-2287.0],
 [-2208.0],
 [-2173.0],
 [-2147.0],
 [-2181.0],
 [-2199.0],
 [-2183.0],
 [-2117.0]]

我开始了一个代码,我可以用另一个词来代替这个词,比如:

filename = "testtext.txt"
with open(filename, 'r+') as f:
    text = f.read()
    text = re.sub('cat', 'dog', text)
    f.seek(0)
    f.write(text)
    text = f.truncate()

但当我想迭代列表时,我会陷入困境:

filename = "testtext.txt"
df_list = df.values.tolist()
global filename
for i in range(len(df_list)):
    with open(filename, 'r+') as f:
        text = f.read()
        text = re.sub('cat', df_list[i], text)
        f.seek(0)
        f.write(text)
        text = f.truncate()
        # here I want to do some operations where I use the text file
        print: ("now working on file" + df_list[i])

如有任何帮助,我们将不胜感激:(

IIUC,下面的代码应该可以完成您的任务。

# Assuming this is your list
lst = [[-2271.0000000000005],
 [-2318.0],
 [-2377.0],
 [-2425.0],
 [-2406.0],
 [-2360.9999999999995],
 [-2287.0],
 [-2208.0],
 [-2173.0],
 [-2147.0],
 [-2181.0],
 [-2199.0],
 [-2183.0],
 [-2117.0]]
# Get file contents
filename = "testtext.txt"
with open(filename, 'r') as f:
    text = f.read()
    
# Perform replacements
for value in lst:
    # looping through values in list
    if 'cat' in text:
        # Perform one replacement of 'cat' with value[0]
        text = text.replace('cat', str(value[0]), 1)  # Use value[0] since value is a list
        # Feedback to user of replacement value
        print(f'Replaced cat with value {value[0]}')
    else:
        break  # 'cat' not found
            
# Write new contents back to file
with open(filename, 'w') as f:
    f.write(text)
 

示例

文件:testtext.txt

<QVariantList name="Filters">
    <QVariantMap>
        <QVariantMap name="Filter">
            <QUrl name="[OPENFILE]FactorImage" value=""/>
            <QUrl name="[OPENFILE]OffsetImage" value=""/>
            <bool name="enabled" value="true"/>
            <double name="factor" value="1"/>
            <QString name="name" value="DSNU"/>
            <int name="offset" value="cat"/>
            <int name="offset" value="cat"/>
            <int name="offset" value="cat"/>
            <int name="offset" value="cat"/>
            <int name="offset" value="cat"/>

运行以上代码后的文件testtext.txt

<QVariantList name="Filters">
    <QVariantMap>
        <QVariantMap name="Filter">
            <QUrl name="[OPENFILE]FactorImage" value=""/>
            <QUrl name="[OPENFILE]OffsetImage" value=""/>
            <bool name="enabled" value="true"/>
            <double name="factor" value="1"/>
            <QString name="name" value="DSNU"/>
            <int name="offset" value="-2271.0000000000005"/>
            <int name="offset" value="-2318.0"/>
            <int name="offset" value="-2377.0"/>
            <int name="offset" value="-2425.0"/>
            <int name="offset" value="-2406.0"/>

最新更新