当在流模式下修改文件时,Python帮助调试错误 [英] Python help to debug an error when modify a file in a stream mode

查看:205
本文介绍了当在流模式下修改文件时,Python帮助调试错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题。我正在读一个文件x,y,z为:

  481492.93 6244326.24 26.56 
481493.03 6244325.60 25.06
481493.17 6244324.68 22.89
481493.50 6244322.52 17.80
481492.84 6244327.05 27.84
481492.90 6244326.66 26.90
481492.86 6244327.16 27.45
481493.48 6244323.08 17.79
481492.80 6244327.80 28.30
481492.94 6244326.84 26.04
..........................

我希望在同一个文件上阅读,修改和写入(没有创建备份文件,因为原始文件超过10GB)

  481492.93 6244326.24 26.56(375,2902)
481493.03 6244325.60 25.06(376,2902)
481493.17 6244324.68 22.89(377,2902)
481493.50 6244322.52 17.80(379,2903)
481492.84 6244327.05 27.84(375,2902)
481492.90 6244326.66 26.90(375,2902)
481492.86 6244327.16 27.45(374,2902)
481493.48 6244323.08 17.79(379,2903)
481492.80 6244327.80 28.30(374,2902)
481492.94 6 244326.84 26.04(375,2902)
..........................

我写了以下方法

  def get_point_grid_id(x, y,x_min,y_max,x_dist,y_dist)
col = int((x - x_min)/ x_dist)
row = int((y_max - y)/ y_dist)
return ,col)

with open(file_temp,r +)as f:
for open in open(file_temp):
x,y,z = line.split()
id = get_point_grid_id(float(x),float(y),origin [0],origin [1],1,1)
element = [x,y,z,id]
newelement =.join(元素中的[str(e))e + p>

当我运行该功能时,我收到以下错误

 追溯(最多最近呼叫最后):
文件< editor selection>,第3行,< module>
ValueError:太多的值解压缩

我想这是原始文件的连接问题



出现错误

 >>> x,y,z = line.split()
追溯(最近的最后一次调用):
文件< interactive input>,第1行,< module>
ValueError:太多值要解压缩

其中 code>是奇怪的

 '481499.82 6244470.31 29.23(231,2909)\\\
'

而不是'481499.82 6244470.31 29.23\\\
'



使用打印行之后打开(file_temp)行: i从新文件运行后获得此打印

  481499.98 6244494.02 34.14 
481499.98 6244494.02 34.14(208,2909)
481499.96 6244471.05 33.39
481499.96 6244471.05 33.39(231,2909)
481499.95 6244471.27 33.46
481499.95 6244471.27 33.46(230,2909)
481499.98 6244473.84 32.72
481499.98 6244473.84 32.72 (228,2909)
481499.98 6244474.07 32.70
481499.98 6244474.07 32.70(228,2909)
481499.97 6244474.28 32.93
481499.97 6244474.28 32.93(227,2909)
481499.88 6244474.40 34.35
481499.88 6244474.40 34.35(227,2909)


解决方案

这只是不行。正如Martijn所说,





文件有缓冲位置。每次阅读一个字符时,缓冲区位置都会前进1.假设您读取了一个10个字符长的行:

 >> ;> myfile = open('some_file.txt')
>>> myfile.tell()#gets缓冲区位置
0
>>> myfile.readline()
'012345678\\\
'

现在缓冲区位置高级 len(line) character:

 >>> myfile.tell()
10

这意味着当你调用 myfile.write(),它开始写在第10位。



II



您根本不能将文字插入到文件中,而不会覆盖某些内容,或者将字符附加到最后(假定缓冲区位置在文件末尾)。



那么你做什么?



你可以创建一个临时文件,同时从你的输入文件读取,并写入你的临时文件。之后(如果你愿意),你可以用原来的文件替换原来的文件:

 以open(input_file)为infile ,open(output_temp_file,w)as outfile:
for infile:
x,y,z = line.split()
new_line =''.join([x,y ,z] + [function_of_xyz(x,y,z)])+'\\\
'
outfile.write(new_line)

您还应查看 csv 模块


I have the following problem. I am reading a file x,y,z as:

481492.93 6244326.24 26.56
481493.03 6244325.60 25.06
481493.17 6244324.68 22.89
481493.50 6244322.52 17.80
481492.84 6244327.05 27.84
481492.90 6244326.66 26.90
481492.86 6244327.16 27.45
481493.48 6244323.08 17.79
481492.80 6244327.80 28.30
481492.94 6244326.84 26.04
..........................

i wish to read, modify, and write on the same file (without create a back-up file because the originals file is more than 10GB)

481492.93 6244326.24 26.56 (375, 2902)
481493.03 6244325.60 25.06 (376, 2902)
481493.17 6244324.68 22.89 (377, 2902)
481493.50 6244322.52 17.80 (379, 2903)
481492.84 6244327.05 27.84 (375, 2902)
481492.90 6244326.66 26.90 (375, 2902)
481492.86 6244327.16 27.45 (374, 2902)
481493.48 6244323.08 17.79 (379, 2903)
481492.80 6244327.80 28.30 (374, 2902)
481492.94 6244326.84 26.04 (375, 2902)
..........................

i wrote the following approach

def get_point_grid_id(x,y,x_min,y_max,x_dist,y_dist):
        col = int((x - x_min)/x_dist)
        row = int((y_max - y)/y_dist)
        return (row, col)

with open(file_temp, "r+") as f:
    for line in open(file_temp):
        x,y,z = line.split()
        id = get_point_grid_id(float(x),float(y),origin[0],origin[1],1,1)
        element = [x,y,z,id]
        newelement = " ".join([str(e) for e in element])+ "\n"
        f.write(newelement)

when i run the function i got the following error

Traceback (most recent call last):
  File "<editor selection>", line 3, in <module>
ValueError: too many values to unpack

i suppose it's a connection problem with the original file

the error appears

>>> x,y,z = line.split()
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
ValueError: too many values to unpack

where line is strange

'481499.82 6244470.31 29.23 (231, 2909)\n' 

instead of '481499.82 6244470.31 29.23\n'

using print line after for line in open(file_temp): i got this print after run from a new file

481499.98 6244494.02 34.14
481499.98 6244494.02 34.14 (208, 2909)
481499.96 6244471.05 33.39
481499.96 6244471.05 33.39 (231, 2909)
481499.95 6244471.27 33.46
481499.95 6244471.27 33.46 (230, 2909)
481499.98 6244473.84 32.72
481499.98 6244473.84 32.72 (228, 2909)
481499.98 6244474.07 32.70
481499.98 6244474.07 32.70 (228, 2909)
481499.97 6244474.28 32.93
481499.97 6244474.28 32.93 (227, 2909)
481499.88 6244474.40 34.35
481499.88 6244474.40 34.35 (227, 2909)

解决方案

This just isn't going to work. As Martijn said,

I

file objects have a buffer position. Every time you read a character the buffer position advances by 1. Suppose you read a line that's 10 characters long:

>>> myfile = open('some_file.txt')
>>> myfile.tell() #gets the buffer position
0
>>> myfile.readline()
'012345678\n'

Now the buffer position is advanced by len(line) characters:

>>> myfile.tell()
10

This means that when you call myfile.write(), it starts writing at position 10.

II

You simply can't "insert" characters into a file without overwriting something, or appending characters to the end (assuming that the buffer position is at the end of the file).

So what do you do?

You can create a temporary file, and simultaneously read from your input file, and write to your temp file. Afterwards (if you should wish), you can replace your original file with the temporary one:

with open(input_file) as infile, open(output_temp_file, "w") as outfile:
    for line in infile:
        x, y, z = line.split()
        new_line = ' '.join([x, y, z] + [function_of_xyz(x, y, z)]) + '\n'
        outfile.write(new_line)

You should also check out the csv module.

这篇关于当在流模式下修改文件时,Python帮助调试错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆