Python新用户问题 - 文件writeline错误 [英] Python new user question - file writeline error

查看:83
本文介绍了Python新用户问题 - 文件writeline错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我是Python&的新手想知道有人可以帮我这个...


我有这个代码:

--------------- -----------

#! / usr / bin / python

import sys

month = {''JAN'':1,''FEB'':2 ,''MAR'':3,''APR'':4,''MAY'':5,''JUN'':6,''JUL'':7,''AUG'':
8,''SEP'':9,''OCT'':10,''NOV'':11,''DEC'':12}

infile = file ('''TVA-0316'',''r'')

outfile = file(''tmp.out'',''w'')


for infile:

item = line.split('','')

dob = item [6] .split(''/'' )

dob = dob [2] +'' - ''+ str(月[dob [1]])+'' - ''+ dob [0]

lbdt = item [8] .split(''/'')

lbdt = lbdt [2] +'' - ''+ str(月[lbdt [1]])+'' - ''+ lbdt [0]

lbrc = item [10] .split(''/'')

lbrc = lbrc [2] +'' - ' '+ str(月[lbrc [1]])+'' - ''+ lbrc [0]

lbrp = item [14] .split(''/'')

lbrp = lbrp [2] +'' - ''+ str(月[lbrp [1]])+' - ''+ lbrp [0]

item [6] = dob

item [8] = lbdt

item [10] = lbrc

item [14] = lbrp

list ='',''。join(item)

outfile.writelines(list)

infile.close

outfile.close

---------------------- -------


数据文件(TVA-0316)如下所示:

---------- -------------------

06-0588,03,701,03701,0000046613,JJB,05 / MAR / 1950,M,20 / NOV / 2006,08:50,21 /

NOV / 2006,V1 ,,, 21 / NOV / 2006,AST,19,U / L,5,40 ,,

06-0588,03,701,03701,0000046613,JJB,05 / MAR / 1950,M,20 / NOV / 2006,08:50,21 /

NOV / 2006,V1 ,,, 21 / NOV / 2006,GGT,34,U / L,11,32,h,

06-0588,03,701,03701,0000046613,JJB,05 / MAR / 1950,M,20 / NOV / 2006,08:50,21 /

NOV / 2006,V1 ,,, 21 / NOV / 2006,ALT,31,U / L,5,29,h,

06-0588,03,701,03701,0000046613,JJB,05 / MAR / 1950,M,20 / NOV / 2006,08:50,21 /

NOV / 2006,V1, ,, 21 / NOV / 2006,ALKP,61,U / L,40,135 ,,

----------------------- ------
/>
基本上我正在阅读每一行并将所有日期字段(05 /

MAR / 1950)转换为不同的格式(1950-03-05)以便加载到MySQL

表。


我有两个问题:

1. outfile没有完成,没有错误信息。当我在python解释器的最后一行检查

时,它已读取并处理了

的最后一行,但输出文件之前停止了。

2.这是在Python中执行此操作的最佳方式吗?

3.(超出范围)有没有办法将此CSV文件直接加载到

MySQL数据中没有转换格式的字段?


谢谢。


James

解决方案

Jamesaécrit:


你好,


我是Python的新手&安培;想知道有人可以帮我这个...


我有这个代码:

--------------- -----------

#! / usr / bin / python

import sys

month = {''JAN'':1,''FEB'':2 ,''MAR'':3,''APR'':4,''MAY'':5,''JUN'':6,''JUL'':7,''AUG'':
8,''SEP'':9,''OCT'':10,''NOV'':11,''DEC'':12}

infile = file ('''TVA-0316'',''r'')

outfile = file(''tmp.out'',''w'')


for infile:

item = line.split('','')



CSV格式?
http://docs.python.org/lib/module-csv .html


dob = item [6] .split(''/'')

dob = dob [2 ] +'' - ''+ str(月[dob [1]])+'' - ''+ dob [0]



为什么使用整数因为这个月的价值决定是否使用

将它们作为字符串?

lbdt = item [8] .split(''/'')

lbdt = lbdt [2] +'' - ''+ str(月[lbdt [1]])+'' - ''+ lbdt [0]

lbrc = item [10] .split(''/'')

lbrc = lbrc [2] +'' - ''+ str(月[lbrc [1]])+'' - ''+ lbrc [0]

lbrp = item [14] .split( ''/'')

lbrp = lbrp [2] +'' - ''+ str(月[lbrp [1]])+'' - ''+ lbrp [0]



这也可能有所帮助:
http://docs.python.org/lib/module-datetime.html


item [6] = dob

item [8] = lbdt

item [10] = lbrc

item [14] = lbrp

list ='',''。join(item)



最好避免使用内置类型名称作为标识符。还有FWIW,这个

不是*列表...


outfile.writelines(list)


您需要file.writeline()或file.write()。你必须手动添加

换行。


infile.close



你实际上并没有*调用* infile.close - 只是在文件上获得了参考

file.close方法。 parens在Python中不是可选的,它们是调用操作符的


outfile.close



同上。


------------------------- ----


数据文件(TVA-0316)如下所示:

------------- ----------------

06-0588,03,701,03701,0000046613,JJB,05 / MAR / 1950,M,20 / NOV / 2006, 08:50,21 /

NOV / 2006,V1 ,,, 21 / NOV / 2006,AST,19,U / L,5,40 ,,

06 -0588,03,701,03701,0000046613,JJB,05 / MAR / 1950,M,20 / NOV / 2006,08:50,21 /

NOV / 2006,V1 ,,, 21 / NOV / 2006,GGT,34,U / L,11,32,h,

06-0588,03,701,03701,0000046613,JJB,05 / MAR / 1950,M,20 / NOV / 2006 ,08:50,21 /

NOV / 2006,V1 ,,, 21 / NOV / 2006,ALT,31,U / L,5,29,h,

06-0588,03,701,03701,0000046613,JJB,05 / MAR / 1950,M,20 / NOV / 2006,08:50,21 /

NOV / 2006,V1 ,,, 21 / NOV / 2006,ALKP,61,U / L,40,135 ,,

-------------------------- ---


基本上我在读g在每一行中并将所有日期字段(05 /

表中。


我有两个问题:

1. outfile没有完成,没有错误信息。当我在python解释器的最后一行检查

时,它已读取并处理了

的最后一行,但输出文件之前停止了。



使用csv模块并干净地关闭文件,然后如果你仍然有问题,请回来。


2.这是在Python中执行此操作的最佳方法吗?



错误...说什么......显然,没有。


2007年2月7日11 :31:32 -0800,James< ci *********** @ gmail.comwrote:


你好,


我是Python&的新手想知道有人可以帮我这个...


我有这个代码:

--------------- -----------


#! / usr / bin / python

import sys

month = {''JAN'':1,''FEB'':2 ,''MAR'':3,''APR'':4,''MAY'':5,''JUN'':6,''JUL'':7,''AUG'':
8,''SEP'':9,''OCT'':10,''NOV'':11,''DEC'':12}

infile = file ('''TVA-0316'',''r'')

outfile = file(''tmp.out'',''w'')


for infile:

item = line.split('','')

dob = item [6] .split(''/'' )

dob = dob [2] +'' - ''+ str(月[dob [1]])+'' - ''+ dob [0]

lbdt = item [8] .split(''/'')

lbdt = lbdt [2] +'' - ''+ str(月[lbdt [1]])+'' - ''+ lbdt [0]

lbrc = item [10] .split(''/'')

lbrc = lbrc [2] +'' - ' '+ str(月[lbrc [1]])+'' - ''+ lbrc [0]

lbrp = item [14] .split(''/'')

lbrp = lbrp [2] +' - ''+ str(月[lbrp [1]])+'' - ''+ lbrp [0]

item [6] = dob

item [8 ] = lbdt

item [10] = lbrc

item [14] = lbrp

list ='',''。join(item )

outfile.writelines(list)

infile.close

outfile.close

----- ------------------------


数据文件(TVA-0316)如下所示:

-----------------------------

06-0588,03,701, 03701,0000046613,JJB,05 / MAR / 1950,M,20 / NOV / 2006,08:50,21 /

NOV / 2006,V1 ,,, 21 / NOV / 2006,AST, 19,U / L,5,40 ,,

06-0588,03,701,03701,0000046613,JJB,05 / MAR / 1950,M,20 / NOV / 2006,08:50,21 /

NOV / 2006,V1 ,,, 21 / NOV / 2006,GGT,34,U / L,11,32,h,

06-0588,03,701 ,03701,0000046613,JJB,05 / MAR / 1950,M,20 / NOV / 2006,08:50,21 /

NOV / 2006,V1 ,,, 21 / NOV / 2006,ALT ,31,U / L,5,29,h,

06-0588,03,701,03701,0000046613,JJB,05 / MAR / 1950,M,20 / NOV / 2006,08:50 ,21 /

NOV / 2006,V1 ,,, 21 / NOV / 2006,ALKP,61,U / L,40, 135 ,,

-----------------------------


基本上我正在阅读每一行并将所有日期字段(05 /

表。


我有两个问题:

1. outfile没有完成,没有错误信息。当我在python解释器的最后一行检查

时,它已读取并处理了

的最后一行,但输出文件之前停止了。

2.这是在Python中执行此操作的最佳方式吗?

3.(超出范围)有没有办法将此CSV文件直接加载到

MySQL数据中没有转换格式的字段?


谢谢。


James


-
http://mail.python.org/mailman/listinfo/ python-list



你的脚本对我有用。我不确定下一步是什么,

对它进行故障排除。您的空白是否可能不是很好?我不得不重新格式化,但我认为这是因为

cut&粘贴是从Gmail工作的。


我通常使用Perl来处理这样的数据,但我不明白为什么

Python不会是一个好的解决方案但是,我会使用正则表达式重新编写

,以查找和替换格式为

日期的部分,而不是将其分成每个字段的变量,每个日期单独更改

,然后将它们重新组合在一起。


至于MySQL如何使用CSV输入格式化日期:我不能

帮助那里,但我相信其他人可以。


我自己很陌生,但如果你想帮助我a

Perl / regex解决方案,我很高兴。就此而言,制作一个
Python / regex解决方案可能对我有好处。让我知道。


Shawn


2月7日下午4:59,Shawn Milo < S ... @ Milochik.comwrote:


2007年2月7日11:31:32 -0800,James< cityhunter ... @ gmail.comwrote :


你好,


我是Python的新手想知道有人可以帮我这个...


我有这个代码:

------- -------------------

#! / usr / bin / python


import sys


month = {''JAN'':1,''FEB'':2,''MAR'':3,''APR'':4,''MAY'':5,''JUN'':6,' 'JUL'':7,''AUG'':

8,''SEP'':9,''OCT'':10,''NOV'':11,''DEC '':12}

infile = file(''TVA-0316'',''r'')

outfile = file(''tmp.out''' ,''w'')


for infile:

item = line.split('',' ')

dob = item [6] .split(''/'')

dob = dob [2] +'' - ''+ str(月[ dob [1]])+'' - ''+ dob [0]

lbdt = item [8] .split(''/'')

lbdt = lbdt [2] +'' - ''+ str(月[lbdt [1]])+'' - ''+ lbdt [0]

lbrc = item [10] .split(' '/'')

lbrc = lbr c [2] +'' - ''+ str(月[lbrc [1]])+'' - ''+ lbrc [0]

lbrp = item [14] .split(' '/'')

lbrp = lbrp [2] +'' - ''+ str(月[lbrp [1]])+'' - ''+ lbrp [0]

item [6] = dob

item [8] = lbdt

item [10] = lbrc

item [14 ] = lbrp

list ='',''。join(item)

outfile.writelines(list)

infile.close

outfile.close

-----------------------------


数据文件(TVA-0316)如下所示:

------------- ----------------

06-0588,03,701,03701,0000046613,JJB,05 / MAR / 1950,M,20 / NOV / 2006, 08:50,21 /

NOV / 2006,V1 ,,, 21 / NOV / 2006,AST,19,U / L,5,40 ,,

06 -0588,03,701,03701,0000046613,JJB,05 / MAR / 1950,M,20 / NOV / 2006,08:50,21 /

NOV / 2006,V1 ,,, 21 / NOV / 2006,GGT,34,U / L,11,32,h,

06-0588,03,701,03701,0000046613,JJB,05 / MAR / 1950,M,20 / NOV / 2006,08:50,21 /

NOV / 2006,V1 ,,, 21 / NOV / 2006,ALT,31,U / L,5,29,h,
06-0588,03,701,03701,0000046613,JJB,05 / MAR / 1950,M,20 / NOV / 2006,08:50,21 /

NOV / 2006,V1 ,,, 21 / NOV / 2006,ALKP,61,U / L,40,135 ,,

---------------------- -------


基本上我正在阅读每一行并转换所有日期字段(05 /

MAR / 1950)以不同格式(1950-03-05)加载到MySQL

表中。


我有两个问题:

1. outfile没有完成,没有错误信息。当我在python解释器的最后一行检查

时,它已读取并处理了

的最后一行,但输出文件之前停止了。

2.这是在Python中执行此操作的最佳方式吗?

3.(超出范围)有没有办法将此CSV文件直接加载到

MySQL数据中字段没有转换格式?


谢谢。


James


-
http://mail.python.org/mailman/listinfo/python-list



你的脚本对我有用。我不确定下一步是什么,

对它进行故障排除。您的空白是否可能不是很好?我不得不重新格式化,但我认为这是因为

cut&粘贴是从Gmail工作的。


我通常使用Perl来处理这样的数据,但我不明白为什么

Python不会是一个好的解决方案但是,我会使用正则表达式重新编写

,以查找和替换格式为

日期的部分,而不是将其分成每个字段的变量,每个日期单独更改

,然后将它们重新组合在一起。


至于MySQL如何使用CSV输入格式化日期:我不能

帮助那里,但我相信其他人可以。


我自己很陌生,但如果你想帮助我a

Perl / regex解决方案,我很高兴。就此而言,制作一个
Python / regex解决方案可能对我有好处。请告诉我。


Shawn



非常感谢您的优惠。

我也是自己来自Perl - 听过很多关于Python的好东西

所以我正在尝试 - 但它似乎比我想象的更难:(


James


Hello,

I''m a newbie to Python & wondering someone can help me with this...

I have this code:
--------------------------
#! /usr/bin/python

import sys

month ={''JAN'':1,''FEB'':2,''MAR'':3,''APR'':4,''MAY'':5,''JUN'':6, ''JUL'':7,''AUG'':
8,''SEP'':9,''OCT'':10,''NOV'':11,''DEC'':12}
infile=file(''TVA-0316'',''r'')
outfile=file(''tmp.out'',''w'')

for line in infile:
item = line.split('','')
dob = item[6].split(''/'')
dob = dob[2]+''-''+str(month[dob[1]])+''-''+dob[0]
lbdt = item[8].split(''/'')
lbdt = lbdt[2]+''-''+str(month[lbdt[1]])+''-''+lbdt[0]
lbrc = item[10].split(''/'')
lbrc = lbrc[2]+''-''+str(month[lbrc[1]])+''-''+lbrc[0]
lbrp = item[14].split(''/'')
lbrp = lbrp[2]+''-''+str(month[lbrp[1]])+''-''+lbrp[0]
item[6] = dob
item[8] = lbdt
item[10]=lbrc
item[14]=lbrp
list = '',''.join(item)
outfile.writelines(list)
infile.close
outfile.close
-----------------------------

And the data file(TVA-0316) looks like this:
-----------------------------
06-0588,03,701,03701,0000046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/
NOV/2006,V1,,,21/NOV/2006,AST,19,U/L,5,40,,
06-0588,03,701,03701,0000046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/
NOV/2006,V1,,,21/NOV/2006,GGT,34,U/L,11,32,h,
06-0588,03,701,03701,0000046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/
NOV/2006,V1,,,21/NOV/2006,ALT,31,U/L,5,29,h,
06-0588,03,701,03701,0000046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/
NOV/2006,V1,,,21/NOV/2006,ALKP,61,U/L,40,135,,
-----------------------------

Basically I''m reading in each line and converting all date fields (05/
MAR/1950) to different format (1950-03-05) in order to load into MySQL
table.

I have two issues:
1. the outfile doesn''t complete with no error message. when I check
the last line in the python interpreter, it has read and processed the
last line, but the output file stopped before.
2. Is this the best way to do this in Python?
3. (Out of scope) is there a way to load this CSV file directly into
MySQL data field without converting the format?

Thank you.

James

解决方案

James a écrit :

Hello,

I''m a newbie to Python & wondering someone can help me with this...

I have this code:
--------------------------
#! /usr/bin/python

import sys

month ={''JAN'':1,''FEB'':2,''MAR'':3,''APR'':4,''MAY'':5,''JUN'':6, ''JUL'':7,''AUG'':
8,''SEP'':9,''OCT'':10,''NOV'':11,''DEC'':12}
infile=file(''TVA-0316'',''r'')
outfile=file(''tmp.out'',''w'')

for line in infile:
item = line.split('','')

CSV format ?
http://docs.python.org/lib/module-csv.html

dob = item[6].split(''/'')
dob = dob[2]+''-''+str(month[dob[1]])+''-''+dob[0]

Why did you use integers as values in the month dict if it''s for using
them as strings ?

lbdt = item[8].split(''/'')
lbdt = lbdt[2]+''-''+str(month[lbdt[1]])+''-''+lbdt[0]
lbrc = item[10].split(''/'')
lbrc = lbrc[2]+''-''+str(month[lbrc[1]])+''-''+lbrc[0]
lbrp = item[14].split(''/'')
lbrp = lbrp[2]+''-''+str(month[lbrp[1]])+''-''+lbrp[0]

This may help too:
http://docs.python.org/lib/module-datetime.html

item[6] = dob
item[8] = lbdt
item[10]=lbrc
item[14]=lbrp
list = '',''.join(item)

Better to avoid using builtin types names as identifiers. And FWIW, this
is *not* a list...

outfile.writelines(list)

You want file.writeline() or file.write(). And you have to manually add
the newline.

infile.close

You''re not actually *calling* infile.close - just getting a reference on
the file.close method. The parens are not optional in Python, they are
the call operator.

outfile.close

Idem.

-----------------------------

And the data file(TVA-0316) looks like this:
-----------------------------
06-0588,03,701,03701,0000046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/
NOV/2006,V1,,,21/NOV/2006,AST,19,U/L,5,40,,
06-0588,03,701,03701,0000046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/
NOV/2006,V1,,,21/NOV/2006,GGT,34,U/L,11,32,h,
06-0588,03,701,03701,0000046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/
NOV/2006,V1,,,21/NOV/2006,ALT,31,U/L,5,29,h,
06-0588,03,701,03701,0000046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/
NOV/2006,V1,,,21/NOV/2006,ALKP,61,U/L,40,135,,
-----------------------------

Basically I''m reading in each line and converting all date fields (05/
MAR/1950) to different format (1950-03-05) in order to load into MySQL
table.

I have two issues:
1. the outfile doesn''t complete with no error message. when I check
the last line in the python interpreter, it has read and processed the
last line, but the output file stopped before.

Use the csv module and cleanly close your files, then come back if you
still have problems.

2. Is this the best way to do this in Python?

Err... What to say... Obviously, no.


On 7 Feb 2007 11:31:32 -0800, James <ci***********@gmail.comwrote:

Hello,

I''m a newbie to Python & wondering someone can help me with this...

I have this code:
--------------------------

#! /usr/bin/python

import sys

month ={''JAN'':1,''FEB'':2,''MAR'':3,''APR'':4,''MAY'':5,''JUN'':6, ''JUL'':7,''AUG'':
8,''SEP'':9,''OCT'':10,''NOV'':11,''DEC'':12}
infile=file(''TVA-0316'',''r'')
outfile=file(''tmp.out'',''w'')

for line in infile:
item = line.split('','')
dob = item[6].split(''/'')
dob = dob[2]+''-''+str(month[dob[1]])+''-''+dob[0]
lbdt = item[8].split(''/'')
lbdt = lbdt[2]+''-''+str(month[lbdt[1]])+''-''+lbdt[0]
lbrc = item[10].split(''/'')
lbrc = lbrc[2]+''-''+str(month[lbrc[1]])+''-''+lbrc[0]
lbrp = item[14].split(''/'')
lbrp = lbrp[2]+''-''+str(month[lbrp[1]])+''-''+lbrp[0]
item[6] = dob
item[8] = lbdt
item[10]=lbrc
item[14]=lbrp
list = '',''.join(item)
outfile.writelines(list)
infile.close
outfile.close
-----------------------------

And the data file(TVA-0316) looks like this:
-----------------------------
06-0588,03,701,03701,0000046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/
NOV/2006,V1,,,21/NOV/2006,AST,19,U/L,5,40,,
06-0588,03,701,03701,0000046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/
NOV/2006,V1,,,21/NOV/2006,GGT,34,U/L,11,32,h,
06-0588,03,701,03701,0000046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/
NOV/2006,V1,,,21/NOV/2006,ALT,31,U/L,5,29,h,
06-0588,03,701,03701,0000046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/
NOV/2006,V1,,,21/NOV/2006,ALKP,61,U/L,40,135,,
-----------------------------

Basically I''m reading in each line and converting all date fields (05/
MAR/1950) to different format (1950-03-05) in order to load into MySQL
table.

I have two issues:
1. the outfile doesn''t complete with no error message. when I check
the last line in the python interpreter, it has read and processed the
last line, but the output file stopped before.
2. Is this the best way to do this in Python?
3. (Out of scope) is there a way to load this CSV file directly into
MySQL data field without converting the format?

Thank you.

James

--
http://mail.python.org/mailman/listinfo/python-list


Your script worked for me. I''m not sure what the next step is in
troubleshooting it. Is it possible that your whitespace isn''t quite
right? I had to reformat it, but I assume it was because of the way
cut & paste worked from Gmail.

I usually use Perl for data stuff like this, but I don''t see why
Python wouldn''t be a great solution. However, I would re-write it
using regexes, to seek and replace sections that are formatted like a
date, rather than breaking it into a variable for each field, changing
each date individually, then putting them back together.

As for how MySQL likes having dates formatted in CSV input: I can''t
help there, but I''m sure someone else can.

I''m pretty new to Python myself, but if you''d like help with a
Perl/regex solution, I''m up for it. For that matter, whipping up a
Python/regex solution would probably be good for me. Let me know.

Shawn


On Feb 7, 4:59 pm, "Shawn Milo" <S...@Milochik.comwrote:

On 7 Feb 2007 11:31:32 -0800, James <cityhunter...@gmail.comwrote:

Hello,

I''m a newbie to Python & wondering someone can help me with this...

I have this code:
--------------------------
#! /usr/bin/python

import sys

month ={''JAN'':1,''FEB'':2,''MAR'':3,''APR'':4,''MAY'':5,''JUN'':6, ''JUL'':7,''AUG'':
8,''SEP'':9,''OCT'':10,''NOV'':11,''DEC'':12}
infile=file(''TVA-0316'',''r'')
outfile=file(''tmp.out'',''w'')

for line in infile:
item = line.split('','')
dob = item[6].split(''/'')
dob = dob[2]+''-''+str(month[dob[1]])+''-''+dob[0]
lbdt = item[8].split(''/'')
lbdt = lbdt[2]+''-''+str(month[lbdt[1]])+''-''+lbdt[0]
lbrc = item[10].split(''/'')
lbrc = lbrc[2]+''-''+str(month[lbrc[1]])+''-''+lbrc[0]
lbrp = item[14].split(''/'')
lbrp = lbrp[2]+''-''+str(month[lbrp[1]])+''-''+lbrp[0]
item[6] = dob
item[8] = lbdt
item[10]=lbrc
item[14]=lbrp
list = '',''.join(item)
outfile.writelines(list)
infile.close
outfile.close
-----------------------------

And the data file(TVA-0316) looks like this:
-----------------------------
06-0588,03,701,03701,0000046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/
NOV/2006,V1,,,21/NOV/2006,AST,19,U/L,5,40,,
06-0588,03,701,03701,0000046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/
NOV/2006,V1,,,21/NOV/2006,GGT,34,U/L,11,32,h,
06-0588,03,701,03701,0000046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/
NOV/2006,V1,,,21/NOV/2006,ALT,31,U/L,5,29,h,
06-0588,03,701,03701,0000046613,JJB,05/MAR/1950,M,20/NOV/2006,08:50,21/
NOV/2006,V1,,,21/NOV/2006,ALKP,61,U/L,40,135,,
-----------------------------

Basically I''m reading in each line and converting all date fields (05/
MAR/1950) to different format (1950-03-05) in order to load into MySQL
table.

I have two issues:
1. the outfile doesn''t complete with no error message. when I check
the last line in the python interpreter, it has read and processed the
last line, but the output file stopped before.
2. Is this the best way to do this in Python?
3. (Out of scope) is there a way to load this CSV file directly into
MySQL data field without converting the format?

Thank you.

James

--
http://mail.python.org/mailman/listinfo/python-list


Your script worked for me. I''m not sure what the next step is in
troubleshooting it. Is it possible that your whitespace isn''t quite
right? I had to reformat it, but I assume it was because of the way
cut & paste worked from Gmail.

I usually use Perl for data stuff like this, but I don''t see why
Python wouldn''t be a great solution. However, I would re-write it
using regexes, to seek and replace sections that are formatted like a
date, rather than breaking it into a variable for each field, changing
each date individually, then putting them back together.

As for how MySQL likes having dates formatted in CSV input: I can''t
help there, but I''m sure someone else can.

I''m pretty new to Python myself, but if you''d like help with a
Perl/regex solution, I''m up for it. For that matter, whipping up a
Python/regex solution would probably be good for me. Let me know.

Shawn

Thank you very much for your kind offer.
I''m also coming from Perl myself - heard many good things about Python
so I''m trying it out - but it seems harder than I thought :(

James


这篇关于Python新用户问题 - 文件writeline错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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