代码之家  ›  专栏  ›  技术社区  ›  GeekJock

Python文件读写

  •  5
  • GeekJock  · 技术社区  · 15 年前

    我正在通过数据库将自定义MSSQL CMS移植到MYSQL-Wordpress。我正在使用Python读取一个带有 \t 划定的列和每行一行。

    我正在读取的文件中有一行类似于:

    1    John Smith    Developer  http://twiiter.com/johns   Chicago, IL
    

    到目前为止,我的Python脚本:

    import sys
    
    fwrite = open('d:/icm_db/wp_sql/wp.users.sql','w')
    
    fread = open('d:/icm_db/users.txt','r')
    
    for line in fread:
        print line;
    
    
    fread.close()
    fwrite.close()
    

    如何“内爆”每一行,以便访问每一列并在其上开展业务?

    我需要在我读的每行生成多个MYSQL insert语句。所以对于读取的每一行,我将生成如下内容:

    INSERT INTO `wp_users` (`ID`, `user_login`, `user_name`) 
    VALUES (line[0], 'line[2]', 'line[3]');
    
    5 回复  |  直到 15 年前
        1
  •  10
  •   Paolo Bergantino    15 年前

    虽然这很容易做到,但使用 csv 单元

    >>> import csv
    >>> reader = csv.reader(open('C:/www/stackoverflow.txt'), delimiter='\t')
    >>> for row in reader:
    ...     print row
    ...
    ['1', 'John Smith', 'Developer', 'http://twiiter.com/johns', 'Chicago, IL']
    ['2', 'John Doe', 'Developer', 'http://whatever.com', 'Tallahassee, FL']
    

        2
  •  1
  •   tom    15 年前

    了解列的确切数量有助于自行记录代码:

    fwrite = open("d:/icm_db/wp_sql/wp.users.sql","w")
    
    for line in open("d:/icm_db/users.txt"):
      name, title, login, location = line.strip().split("\t")
    
      # Double up on those single quotes to avoid nasty SQL!
      safe_name = name.replace("'","''")
      safe_login = name.replace("'","''")
    
      # ID field is primary key and will auto-increment
      fwrite.write( "INSERT INTO `wp_users` (`user_login`, `user_name`) " )
      fwrite.write( "VALUES ('%s','%s');\n" % (safe_name,safe_login) )
    
        3
  •  0
  •   othercriteria    15 年前

    Python标准库有一个用于 CSV (comma separated value) file reading and writing 可以使其在选项卡分隔的文件(如您的文件)上工作。这项任务可能做得太过火了。

        4
  •  0
  •   Etienne Perot    15 年前

    您可能想要的是这样的: data=line.split("\t")
    它将为您提供一个很好的序列对象。
    print line; )

    正如戴夫指出的,这可能会留下一条新线。在拆分之前在线调用strip(),如下所示: line.strip().split("\t")

        5
  •  0
  •   sqram    15 年前
    fwrite = open('/home/lyrae/Desktop/E/wp.users.sql','a')
    fread = open('/home/lyrae/Desktop/E/users.txt','r')
    
    for line in fread:
        line = line.split("\t")
        fwrite.write("insert into wp_users ( ID, user_login, user_name ) values (%s, '%s', '%s')\n" % (line[0], line[1], line[2]))
    
    fread.close()
    fwrite.close()
    

    假设users.txt为:

    1   John Smith  Developer   http://twiiter.com/johns    Chicago, IL
    2   Billy bob   Developer   http://twiiter.com/johns    Chicago, IL
    3   John Smith  Developer   http://twiiter.com/johns    Chicago, IL
    

    insert into wp_users ( ID, user_login, user_name ) values (1, 'John Smith', 'Developer')
    insert into wp_users ( ID, user_login, user_name ) values (2, 'Billy bob', 'Developer')
    insert into wp_users ( ID, user_login, user_name ) values (3, 'John Smith', 'Developer')
    

    假设只有1个选项卡分隔id、名称和位置