[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] [已解决]批处理怎样把不以引号开头的行合并到上一行行尾?

比如有一个文件a.txt内容大致如下:

情景一:
"1","12","123","123
4"
"A","AB","ABC","ABCD"
执行脚本后内容能够修改为
"1","12","123","1234"
"A","AB","ABC","ABCD"

情景二:
"1","12","123","
4"
"A","AB","ABC","ABCD"
执行脚本后内容能够修改为
"1","12","123","4"
"A","AB","ABC","ABCD"

情景三(出现于27L的这种情况属于正确的数据)
"1","12","123",
"A","AB","ABC",

能处理情景一、二、三的代码在19L
就算处理1.7G的测试文件依然很快,但是对于大于等于两个字段有换行符就不能处理了,虽然不会报错,比如下面

情景四:
"1","
12","12
3",,,
"1","12","123",,,
执行脚本后内容修改为
"1","12","123",,,
"1","12","123",,,

能处理情景一、二、三、四的代码在44L和57L
44L的bat+js代码无法处理测试用的文件1.7G,原因未知, win2003的兼容性目前未知
57L的python代码能处测试用的文件1.7G,耗时140s,这应该与机器性能有关,仅作参考




P.S:
1. PyInstaller打包的exe在2003上跑会有问题,老老实实装python的好
2. 本机(win7)装了python3.5_X64编译出来的代码在2003下怎么弄都有问题,2003换python2.7.10_X64可行。X86在读取大文件的时候会报错内存错误
1

评分人数

    • Batcher: 感谢给帖子标题标注[已解决]字样PB + 2

回复 2# 回家路上

a.txt的内容并没有变

TOP

回复 3# WHY


第一段代码执行后a.txt并没有变
第二段。。没看懂~

TOP

回复 4# 依山居


    思路是对的,然而  'fr' 不是内部或外部命令,也不是可运行的程序或批处理文件。

TOP

回复 8# 依山居


Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
========================== RESTART: F:\BAT\tt\tt.py ==========================
Traceback (most recent call last):
  File "F:\BAT\tt\tt.py", line 11, in <module>
    newtxt=[txt[r][:]+txt[r+1][:] if ('\"' not in txt[r][-1]) else txt[r] for r in range(rn) ]
  File "F:\BAT\tt\tt.py", line 11, in <listcomp>
    newtxt=[txt[r][:]+txt[r+1][:] if ('\"' not in txt[r][-1]) else txt[r] for r in range(rn) ]
IndexError: string index out of range
>>>

TOP

回复 10# 依山居


还真有 = = 。。莫名奇妙最后多了一行。。。

TOP

回复 10# 依山居


   最后一行的空行。。这个貌似没办法避免。。导出的文本数据文件都有这样的问题。。。可以在脚本里规避掉最后一行空行的问题吗

TOP

回复 13# CrLf


不大可能出现错误的断行 - -

实际情况是 db2导出del文件,中间某个字段的某条记录中可能包含有换行符,然后就出现了我说的情况
本来应该在导出的时候,或者直接在数据库层面就能直接解决。但现在的实际情况是。。需要我们自己通过操作这个del文件来解决
0 0 蛋疼的不行

TOP

回复 15# 依山居

谢了~  可用~

遍历路径下所有的del文件我就自己百度好了~

TOP

本帖最后由 qq253040425 于 2015-11-18 17:40 编辑

回复 18# 依山居


谢谢大神帮助,已搞定!虽然主体部分的逻辑完全看不懂!但是测试能用了! 用百万级的.del文件测试过了,没啥问题的样子
  1. import os
  2. import time
  3. import datetime
  4. print (time.strftime("%Y-%m-%d %H:%M:%S %p", time.localtime()))
  5. print ('Check start, remove the ok file ... ')
  6. lst_last=datetime.date(datetime.date.today().year,datetime.date.today().month,1)-datetime.timedelta(1)
  7. workdir='D:\\IMPDATA\\FTPFILE\\'+lst_last.strftime('%Y-%m-%d')+'\\cqcs\\817'
  8. okfile=workdir+'\check.ok'
  9. if os.path.exists(okfile):
  10.     os.remove(okfile)
  11. newtxt=[]
  12. for dirpath,dirs,files in os.walk(workdir):
  13.    
  14.     for file in files:
  15.         
  16.         if os.path.splitext(file)[1] == '.del':
  17.             
  18.             vfname=os.path.join(dirpath,file)
  19.             print ('Check file: ' + vfname)
  20.             
  21.             with open(vfname) as f:
  22.                 txt=f.readlines()
  23.                 txt=[r.rstrip() for r in txt]
  24.                 rn=len(txt)
  25.                 print("Rowscount:",rn)
  26.                 newtxt=[txt[r-1][:]+txt[r][:] if ( ('\"' not in txt[r][0]) )  else txt[r]  for r in range(rn) ]
  27.                 newtxt=[r+"\n" for r in newtxt if r.count('\"')%2==0]
  28.                 f.close()
  29.                
  30.             nfname= vfname+'_new'            
  31.             with open(nfname,"w+") as f:
  32.                 f.writelines(newtxt)
  33.                 f.close()
  34.                
  35.             os.remove(vfname)
  36.             os.rename(nfname,vfname)
  37. print ('Check over, touch the ok file ...')
  38. with open(okfile,'w+') as f:
  39.     f.writelines('')
  40.     f.close()
  41. print (time.strftime("%Y-%m-%d %H:%M:%S %p", time.localtime()))         
复制代码

TOP

回复 20# 依山居


我在官网下载的64位的python3.5 = = 能不能教下如何封装exe

TOP

回复 13# CrLf


不好意思 - - 真的出现这种断行了

但是数据是正常的数据,不是错误数据

TOP

本帖最后由 qq253040425 于 2015-11-18 11:38 编辑

回复 25# 依山居

大神!!
    出现问题了。。

数据中有
"LS81702201409060006659","20",,"817029700","817029700","2","9","",,,,,
不以“结尾,以,结尾的数据,这样的子的数据是正确的数据,

Traceback (most recent call last):
  File "F:\BAT\tt\check_del_here.py", line 29, in <module>
    newtxt=[txt[r][:]+txt[r+1][:] if ('\"' not in txt[r][-1]) else txt[r] for r in range(rn) ]
  File "F:\BAT\tt\check_del_here.py", line 29, in <listcomp>
    newtxt=[txt[r][:]+txt[r+1][:] if ('\"' not in txt[r][-1]) else txt[r] for r in range(rn) ]
IndexError: list index out of range

TOP

回复 28# 依山居


    嗯 谢谢提醒

del文件的内容和我那个txt的内容是一样的,del其实也是文本文件

我觉得固定列数并不科学,我感觉

newtxt=[txt[r][:]+txt[r+1][:] if ('\"' not in txt[r][-1]) else txt[r] for r in range(rn) ]
newtxt=[r+"\n" for r in newtxt if '\"' in r[0]]

这一段修改一下,因为实际文件中的错误情况只有一种: n行的最后一个字段不以”结尾且n+1行不以”开头

TOP

回复 30# 依山居


   发现一个解决一个吧

TOP

返回列表