Python 为什么在每个连续打开的文件上附加相同的行?

Python 为什么在每个连续打开的文件上附加相同的行?,python,csv,duplicates,Python,Csv,Duplicates,我的原始.csv文件有数千行,但为了清晰起见,只显示了一行。我对示例行创建了4个连续的更改。Python将做出正确的更改,但会附加这些更改。我没有一行包含所有更改,而是有4行,每行代表4个更改中的一个更改 我尝试使用r,r+,w,a模式,每个模式基本上都是一样的。尝试使用单个打印语句。一个print语句只保存一行,但只保存一个更改 import re with open("orig.csv","r") as fi: contents=fi.readlines() with open(

我的原始.csv文件有数千行,但为了清晰起见,只显示了一行。我对示例行创建了4个连续的更改。Python将做出正确的更改,但会附加这些更改。我没有一行包含所有更改,而是有4行,每行代表4个更改中的一个更改

我尝试使用
r
r+
w
a
模式,每个模式基本上都是一样的。尝试使用单个
打印
语句。一个
print
语句只保存一行,但只保存一个更改

import re

with open("orig.csv","r") as fi:
    contents=fi.readlines()

with open("output.csv","r+") as fi:
    for line in contents:
        fi = re.sub(r"<.*?>","",line)
        print(fi)

with open("orig.csv","r") as fi:
    contents=fi.readlines()
with open("output.csv","r+") as fi:
    for line in contents:
        fi=re.sub(r",[^,]+,Skipped,",",",line)
        print(fi)

with open("orig.csv","r") as fi:
    contents=fi.readlines()
with open("output.csv","r+") as fi:
    for line in contents:
        fi=re.sub(r",[^,]+,Done,",",",line)
        print(fi)

with open("orig.csv","r") as fi:
    contents=fi.readlines()

with open("output.csv","r+") as fi:
    for line in contents:
        fi=re.sub(r",,",",",line)
        print(fi)

在将其写入文件之前,您应该只对
-循环使用一个
,只读取一行,并在此行上使用所有
re.sub

import re

with open("orig.csv", "r") as file_in:
    contents = file_in.readlines()

with open("output.csv", "w") as file_out:
    for line in contents:
        line = re.sub(r"<.*?>", "", line)
        line = re.sub(r",[^,]+,Skipped,", ",", line)
        line = re.sub(r",[^,]+,Done,", ",", line)
        line = re.sub(r",,", ",", line)
        line = re.sub(r"\s+", ",", line) # remove spaces

        file_out.write(line) # write in file

        print(line) # display on screen
重新导入
打开(“orig.csv”、“r”)作为文件
contents=file_in.readlines()
打开(“output.csv”、“w”)作为文件输出:
对于行内内容:
line=re.sub(r“”,第行)
line=re.sub(r“,[^,]+,跳过,,,,”行)
line=re.sub(r“,[^,]+,Done“,,”,line)
行=re.sub(r“,”,“,”行)
line=re.sub(r“\s+”,“,”,line)#删除空格
文件输出。写入(行)#写入文件
打印(行)#在屏幕上显示

每个人都可以轻松复制和测试的示例

import re

data = "<UUT><H s='12' v='2.8'/>    <V t='s' s='2'/>Profile Debug   <V t='s' s='2'/>Cycle   Normal  <V t='s' s='2'/>PMVer   14.0.1.103  <V t='s' s='2'/>SeqFileVer  1.6.0.0 <V t='s' s='2'/>User    administrator   <V t='s' s='2'/>Station TS-0357A    <V t='s' s='2'/>Socket  0   <V t='s' s='2'/>Date    9/10/2018   <V t='s' s='2'/>Time    17:23:51    <V t='n' s='2'/>CycleTime   0   <V t='s' s='2'/>Status  Failed  <V t='s' s='2'/>MAC_Address f8dc7a128189    <R s='42'/> <S t='a' s='3'/>CycleTimes  Done<S t='a' s='3'/>DEBUG_PRODUCTION_FALSE  Skipped<S t='a' s='3'/>DEBUG_TROUBLESHOOTING_TRUE"
contents = data.split('\n')

with open("output.csv", "w") as file_out:
    for line in contents:
        line = re.sub(r"<.*?>", "", line)
        line = re.sub(r",[^,]+,Skipped,", ",", line)
        line = re.sub(r",[^,]+,Done,", ",", line)
        line = re.sub(r",,", ",", line)
        line = re.sub(r"\s+", ",", line) # remove spaces
        file_out.write(line)
        print(line)
重新导入
data=“Profile Debug Cycle Normal PMVer 14.0.1.103 SeqFileVer 1.6.0.0用户管理员站TS-0357A Socket 0 Date 9/10/2018时间17:23:51 CycleTime 0 Status Failed MAC\u Address f8dc7a128189 CycleTime DoneDEBUG\u PRODUCTION\u FALSE skippeddedbug\u problems
contents=data.split('\n')
打开(“output.csv”、“w”)作为文件输出:
对于行内内容:
line=re.sub(r“”,第行)
line=re.sub(r“,[^,]+,跳过,,,,”行)
line=re.sub(r“,[^,]+,Done“,,”,line)
行=re.sub(r“,”,“,”行)
line=re.sub(r“\s+”,“,”,line)#删除空格
文件输出。写入(行)
打印(行)

我在原始数据中没有看到任何逗号。您不应该使用csv来解析csv文件吗?也许您应该只使用一个
来进行
循环,只读取一次行,并在这一行中进行所有更改,然后只打印一次。您的代码从不将其写回文件,因此您总是从文件中获得相同的不变行。您使用
fi
作为指向文件的指针,因此不要将
re
的结果分配到此变量,因为这样您就没有访问文件的权限。最好返回到
line
-
line=re.sub(r“,[^,]+,跳过,”,“,”,line)
line=re.sub(…另一个…,行)
。并使用
fi.write(line)
写入文件。您的python更正是95%的答案。如果按原样打开csv文件,数据将显示在一行上。如果我将其重定向到project.py>output.csv,则在Excel中打开时,数据将填充文件的另一行。在Excel中打开时,可能必须将
设置为分隔符,或者可能必须使用
而不是代码中的