Writelines写入没有换行符的行,只需填写文件.. [英] Writelines writes lines without newline, Just fills the file..
问题描述
列表是分隔线的列表,行应该写入文件,如下所示:
123 | GSV | Weather_Mean | hello | joe | 43.45
122 | GEV | temp_Mean | hello | joe | 23.45
124 | GSI | Weather_Mean | hello | Mike | 47.45
但是它们写下这行啊hhh:
123 | GSV | Weather_Mean | hello | joe | 43.45122 | GEV | temp_Mean | hello | joe | 23.45124 | GSI | Weather_Mean | hello | Mike | 47.45
这个程序将所有的行都写成像一行,没有任何换行符..这伤害了我很多,我必须弄清楚如何扭转这个,但无论如何,我的程序在哪里错了这里?我认为写行应该将文字写在文件上,而不是将所有内容写入一行。
fr = open(sys。 argv [1],'r')#源文件
fw = open(sys.argv [2] +/ masked _+ sys.argv [1],'w')#目标目录位置
在fr中的行
line = line.strip()
if line ==:
continue
columns = line.strip()。split ('|')
如果列[0] .find(@)> 1:
looking_for = columns [0]#这是我们需要搜索
else:
looking_for =Dummy@dummy.com
如果在d中查找_ $ b#默认情况下,迭代字典将返回键
new_line = d [looking_for] +'|'+'|'.join(columns [1:])
line_list.append(new_line)
else:
new_idx = str(len(d)+1)
d [looking_for] = new_idx
kv = open(sys.argv [3],'a' b $ b kv.write(looking_for ++ new_idx +'\\\
')
kv.close()
new_line = d [looking_for] +'|'+'|'.join(columns [ 1:])
line_list.append(new_line)
fw.writelines(line_list)
对于新手来说,这实际上是一个很常见的问题,特别是在标准库和流行的第三方库中,一些阅读功能会删除新行,但几乎没有写函数(除了 log
相关的东西)添加它们。
所以,有很多Python代码可以这样做:
fw.write('\\\
'.join(line_list)+'\\\
')
或
fw.write(line +'\\\
'for line in line_list)
一个是正确的,当然你甚至可以编写自己的写作,包括它的新线程功能...
但是,如果你不能避免,你应该这样做。 / p>
如果您可以首先创建/保留换行符,则更好 - 如Greg Hewgill的建议:
line_list.append(new_line +\\\
)
如果您可以在比原始行文本更高级别工作,甚至使用 csv 模块在标准的li如esuaro所说。
例如,在定义 fw
之后,您可以执行以下操作:
cw = csv.writer(fw,delimiter ='|')
然后,而不是这样:
new_line = d [looking_for] +'|'+' |'.join(columns [1:])
line_list.append(new_line)
你这样做:
row_list.append(d [looking_for] + columns [1:])
最后,而不是这样:
fw.writelines(line_list)
你这样做:
cw.writerows(row_list)
最后,你的设计是打开一个文件,然后建立一个行列表来添加到文件中,然后一次写入。如果你要打开文件顶部,为什么不只是写一行一行?无论您使用简单的写作还是使用 csv.writer
,这将使您的生活更简单,您的代码更容易阅读。 (有时可以简单,有效率或正确地写入文件全部是一次的 - 但是一旦将打开
一直移到另一端程序从写入
,你几乎都失去了一切的好处。)
I have a program that writes a list to a file. The list is a list of pipe delimited lines and the lines should be written to the file like this:
123|GSV|Weather_Mean|hello|joe|43.45
122|GEV|temp_Mean|hello|joe|23.45
124|GSI|Weather_Mean|hello|Mike|47.45
BUT it wrote them line this ahhhh:
123|GSV|Weather_Mean|hello|joe|43.45122|GEV|temp_Mean|hello|joe|23.45124|GSI|Weather_Mean|hello|Mike|47.45
This program wrote all the lines into like one line without any line breaks.. This hurts me a lot and I gotta figure-out how to reverse this but anyway, where is my program wrong here? I thought write lines should write lines down the file rather than just write everything to one line..
fr = open(sys.argv[1], 'r') # source file
fw = open(sys.argv[2]+"/masked_"+sys.argv[1], 'w') # Target Directory Location
for line in fr:
line = line.strip()
if line == "":
continue
columns = line.strip().split('|')
if columns[0].find("@") > 1:
looking_for = columns[0] # this is what we need to search
else:
looking_for = "Dummy@dummy.com"
if looking_for in d:
# by default, iterating over a dictionary will return keys
new_line = d[looking_for]+'|'+'|'.join(columns[1:])
line_list.append(new_line)
else:
new_idx = str(len(d)+1)
d[looking_for] = new_idx
kv = open(sys.argv[3], 'a')
kv.write(looking_for+" "+new_idx+'\n')
kv.close()
new_line = d[looking_for]+'|'+'|'.join(columns[1:])
line_list.append(new_line)
fw.writelines(line_list)
This is actually a pretty common problem for newcomers to Python—especially since, across the standard library and popular third-party libraries, some reading functions strip out newlines, but almost no writing functions (except the log
-related stuff) add them.
So, there's a lot of Python code out there that does things like:
fw.write('\n'.join(line_list) + '\n')
or
fw.write(line + '\n' for line in line_list)
Either one is correct, and of course you could even write your own writelinesWithNewlines function that wraps it up…
But you should only do this if you can't avoid it.
It's better if you can create/keep the newlines in the first place—as in Greg Hewgill's suggestions:
line_list.append(new_line + "\n")
And it's even better if you can work at a higher level than raw lines of text, e.g., by using the csv module in the standard library, as esuaro suggests.
For example, right after defining fw
, you might do this:
cw = csv.writer(fw, delimiter='|')
Then, instead of this:
new_line = d[looking_for]+'|'+'|'.join(columns[1:])
line_list.append(new_line)
You do this:
row_list.append(d[looking_for] + columns[1:])
And at the end, instead of this:
fw.writelines(line_list)
You do this:
cw.writerows(row_list)
Finally, your design is "open a file, then build up a list of lines to add to the file, then write them all at once". If you're going to open the file up top, why not just write the lines one by one? Whether you're using simple writes or a csv.writer
, it'll make your life simpler, and your code easier to read. (Sometimes there can be simplicity, efficiency, or correctness reasons to write a file all at once—but once you've moved the open
all the way to the opposite end of the program from the write
, you've pretty much lost any benefits of all-at-once.)
这篇关于Writelines写入没有换行符的行,只需填写文件..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!