简体   繁体   English

Python文件读取+写入

[英]Python File Read + Write

I am working on porting over a database from a custom MSSQL CMS to MYSQL - Wordpress. 我正在努力将数据库从自定义MSSQL CMS移植到MYSQL - Wordpress。 I am using Python to read a txt file with \\t delineated columns and one row per line. 我使用Python来读取带有\\t描述列的txt文件,每行一行。

I am trying to write a Python script that will read this file (fread) and [eventually] create a MYSSQL ready .sql file with insert statements. 我正在尝试编写一个Python脚本,它将读取此文件(fread)并[最终]创建一个带有insert语句的MYSSQL ready .sql文件。

A line in the file I'm reading looks something like: 我正在阅读的文件中的一行看起来像:

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

My Python script so far: 到目前为止我的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()

How can I "implode" each line so I can access each column and do business on it? 我如何“内爆”每一行,以便我可以访问每一栏并开展业务?

I need to generate multiple MYSQL insert statements per line I read. 我需要为每行读取生成多个MYSQL插入语句。 So... for each line read, I'd generate something like: 所以...对于每一行读取,我会生成如下内容:

INSERT INTO `wp_users` (`ID`, `user_login`, `user_name`) 
VALUES (line[0], 'line[2]', 'line[3]');

Although this is easily doable, it does become easier with the csv module. 虽然这很容易实现,但使用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']

Also, as pointed out, semicolons are not needed in Python. 另外,正如所指出的,Python中不需要分号。 Try to kick that habit :) 试着踢那个习惯:)

Knowing the exact number of columns helps self document your code: 知道确切的列数有助于自我记录您的代码:

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) )

What you probably want is something like this: data=line.split("\\t") 你可能想要的是这样的: data=line.split("\\t")
It'll give you a nice sequence object to work with. 它会为你提供一个很好的序列对象。
(By the way, no need for semicolons in Python. There's one here: print line; ) (顺便说一句,Python中不需要分号。这里有一个: print line;

As Dave pointed out, this might leave a newline in there. 正如戴夫所指出的,这可能会在那里留下新的界限。 Call strip() on line before splitting, like so: line.strip().split("\\t") 在拆分之前调用strip(),就像这样: line.strip().split("\\t")

The Python Standard Library has a module for CSV (comma separated value) file reading and writing that can be made to work on tab separated files like your one. Python标准库有一个用于CSV(逗号分隔值)文件读取和写入的模块 ,可以对像您这样的制表符分隔文件进行操作。 It's probably overkill for this task. 这项任务可能有些过分。

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()

Assuming users.txt is: 假设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

wp.users.sql will look like: wp.​​users.sql看起来像:

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')

Assuming only 1 tab separates the id, name, position 假设只有1个制表符分隔id,name,position

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM