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] = dobitem[8] = lbdtitem[10]=lbrcitem[14]=lbrplist = '',''.join(item)outfile.writelines(list)infile.closeoutfile.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 MySQLtable. I have two issues:1. the outfile doesn''t complete with no error message. when I checkthe last line in the python interpreter, it has read and processed thelast 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 intoMySQL 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/pythonimport sysmonth ={''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 usingthem 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, thisis *not* a list... outfile.writelines(list)You want file.writeline() or file.write(). And you have to manually addthe newline. infile.closeYou''re not actually *calling* infile.close - just getting a reference onthe file.close method. The parens are not optional in Python, they arethe call operator. outfile.closeIdem. -----------------------------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 MySQLtable.I have two issues:1. the outfile doesn''t complete with no error message. when I checkthe last line in the python interpreter, it has read and processed thelast line, but the output file stopped before.Use the csv module and cleanly close your files, then come back if youstill 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/pythonimport sysmonth ={''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.closeoutfile.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 MySQLtable.I have two issues:1. the outfile doesn''t complete with no error message. when I checkthe last line in the python interpreter, it has read and processed thelast 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 intoMySQL 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 introubleshooting it. Is it possible that your whitespace isn''t quiteright? I had to reformat it, but I assume it was because of the waycut & paste worked from Gmail. I usually use Perl for data stuff like this, but I don''t see whyPython wouldn''t be a great solution. However, I would re-write itusing regexes, to seek and replace sections that are formatted like adate, rather than breaking it into a variable for each field, changingeach date individually, then putting them back together. As for how MySQL likes having dates formatted in CSV input: I can''thelp there, but I''m sure someone else can. I''m pretty new to Python myself, but if you''d like help with aPerl/regex solution, I''m up for it. For that matter, whipping up aPython/regex solution would probably be good for me. Let me know. ShawnOn Feb 7, 4:59 pm, "Shawn Milo" <[email protected]:On 7 Feb 2007 11:31:32 -0800, James <[email protected]: 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 introubleshooting it. Is it possible that your whitespace isn''t quiteright? I had to reformat it, but I assume it was because of the waycut & paste worked from Gmail.I usually use Perl for data stuff like this, but I don''t see whyPython wouldn''t be a great solution. However, I would re-write itusing regexes, to seek and replace sections that are formatted like adate, rather than breaking it into a variable for each field, changingeach date individually, then putting them back together.As for how MySQL likes having dates formatted in CSV input: I can''thelp there, but I''m sure someone else can.I''m pretty new to Python myself, but if you''d like help with aPerl/regex solution, I''m up for it. For that matter, whipping up aPython/regex solution would probably be good for me. Let me know.ShawnThank you very much for your kind offer.I''m also coming from Perl myself - heard many good things about Pythonso I''m trying it out - but it seems harder than I thought :( James 这篇关于Python新用户问题 - 文件writeline错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-19 07:58