迭代文件时出错 - Python

时间:2015-12-31 02:51:44

标签: python string readline

我正在尝试使用长字符串迭代txt文件并删除双引号(“)和逗号(,)并将其写入新文件但它仍然出错。请帮忙。

代码:

from sys import argv

script, filename = argv

long_String = ""

for line in filename.readline():
    long_String += line
    long_String += " "


for x in long_String:
    if (x = '\"'):
        x = ""

    if x = ",":
        x = ""

filename2 = "print.txt"

target = open (filename2, 'a')
target.write(long_string)
target.close()

3 个答案:

答案 0 :(得分:1)

您有一些错误:

res <- merge(df, stack(mget(c('fruits', 'nuts', 'veggies'))),
               by.x='code', by.y='values')
names(res)[3] <- 'groups'
res
#  code Values   groups
#1  217      0 veggies
#2  220      0    nuts
#3  221      0  fruits
#4  222 129906  fruits
#5  223      0  fruits
#6  224      0    nuts
#7  225      0 veggies

应该是:

target.write(long_string)

target.write(long_String)

应该是:

if (x = '\"'):

同样:

if (x == '\"'):

否则,您可以轻松使用re.sub

if x = ",":

答案 1 :(得分:0)

您的比较需要=,而不是// Attribution: http://paulbourke.net/geometry/pointlineplane/ // p0 & p1 are points on a first line // p2 & p3 are points on a second line // returns the intersection point (or null if the lines don't intersect) function line2lineIntersection(p0,p1,p2,p3) { var unknownA = (p3.x-p2.x) * (p0.y-p2.y) - (p3.y-p2.y) * (p0.x-p2.x); var unknownB = (p1.x-p0.x) * (p0.y-p2.y) - (p1.y-p0.y) * (p0.x-p2.x); var denominator = (p3.y-p2.y) * (p1.x-p0.x) - (p3.x-p2.x) * (p1.y-p0.y); // Test if Coincident // If the denominator and numerator for the ua and ub are 0 // then the two lines are coincident. if(unknownA==0 && unknownB==0 && denominator==0){return(null);} // Test if Parallel // If the denominator for the equations for ua and ub is 0 // then the two lines are parallel. if (denominator == 0) return null; // If the intersection of line segments is required // then it is only necessary to test if ua and ub lie between 0 and 1. // Whichever one lies within that range then the corresponding // line segment contains the intersection point. // If both lie within the range of 0 to 1 then // the intersection point is within both line segments. unknownA /= denominator; unknownB /= denominator; var isIntersecting=(unknownA>=0 && unknownA<=1 && unknownB>=0 && unknownB<=1) if(!isIntersecting){return(null);} return({ x: p0.x + unknownA * (p1.x-p0.x), y: p0.y + unknownA * (p1.y-p0.y) }); }

答案 2 :(得分:0)

您可以使用字符串替换

long_String = long_String.replace('"','')
long_String = long_String.replace(',','')

它将用空

替换双引号(“)和逗号(,)