python csv copy column

I have a file containing following first_name,last_name,uid,email,dep_code,dep_name john,smith,jsmith,jsmith@gmail.com,finance,21230 john,king,jking,jjing@gmail.com,human resource,31230 I want to copy column "email" and create a new column "email2" and then replace gmail.com from column email2 to hotmail.com I'm new to python so need help from experts, I tried few scri

python csv复制列

我有一个包含以下内容的文件 first_name,last_name,uid,email,dep_code,dep_name john,smith,jsmith,jsmith@gmail.com,finance,21230 john,king,jking,jjing@gmail.com,human resource,31230 我想复制列“电子邮件”,并创建一个新的列“email2”,然后从列email2替换gmail.com到hotmail.com 我是python的新手,所以需要专家的帮助,我尝试了几个脚本,但是如果有更好的方法去做,那么请让我知道。 原始文件包含60000行。 with

Avoiding quotes around row of string data with csv.writer

I am trying to write a row of strings (it's the header row) to a csv file using csv.writer as follows: mbfile=open(pathtodata + "mbsensors.csv", "ab") writer=csv.writer(mbfile) writer.writerow(["ESReadings"]) i=1 tempstring='DateTime' while i<=numsensors: tempstring=tempstring+','+ "%02d"%i +'.Cooked' i=i+1 print tempstring writer.writerow([tempstring]) mbfile.close (I don

避免使用csv.writer引用字符串数据行

我正在尝试使用csv.writer将一行字符串(它是标题行)写入一个csv文件,如下所示: mbfile=open(pathtodata + "mbsensors.csv", "ab") writer=csv.writer(mbfile) writer.writerow(["ESReadings"]) i=1 tempstring='DateTime' while i<=numsensors: tempstring=tempstring+','+ "%02d"%i +'.Cooked' i=i+1 print tempstring writer.writerow([tempstring]) mbfile.close (我不知道为什么第一行缩进 - 这不像我

Python CSV Writer trimming leading zeros

I am reading in a csv file in python, changing a few values, and writing it back out. I read in the file using: bomReader = csv.reader( open( args.filename, 'rb' ), delimiter=',' ) for row in bomReader: #do stuff One of the fields contains values such as 0406 , where the leading zero is important. After reading the csv file, if I print this field using say print row[2] , it displays corre

Python CSV编写器修剪前导零

我正在阅读python中的csv文件,更改了一些值,并将其写回。 我在文件中使用: bomReader = csv.reader( open( args.filename, 'rb' ), delimiter=',' ) for row in bomReader: #do stuff 其中一个字段包含0406 ,其中前导零是重要的。 在读取csv文件后,如果使用say print row[2]打印此字段,则它会以前导0正确显示。 但是,当我将其写回新的csv文件时,输出没有前导零。 我认为这是因为它被认为是一个整数,前导零被

How do quickly search through a .csv file in Python

I'm reading a 6 million entry .csv file with Python, and I want to be able to search through this file for a particular entry. Are there any tricks to search the entire file? Should you read the whole thing into a dictionary or should you perform a search every time? I tried loading it into a dictionary but that took ages so I'm currently searching through the whole file every time wh

如何快速搜索Python中的.csv文件

我正在用Python读取一个600万条.csv文件,我希望能够搜索这个文件以查找特定条目。 是否有任何技巧来搜索整个文件? 你应该把整个东西都读成字典吗?还是每次都要进行搜索? 我试着将它加载到字典中,但这花了很长时间,所以我每次都在搜索整个文件,这看起来很浪费。 我可以利用该列表按字母顺序排列吗? (例如,如果搜索词以“b”开头,那么我只从包含以“b”开头的第一个词到包含以“b”开头的最后一个词的那一行)搜索。

Print pdf file to "Adobe pdf" printer using python 2.7.5

I am trying to print a pdf file to the "Adobe pdf" printer from within python 2.7.5 script. By doing so, the file size is drastically reduced. File name and path is: pdfdoc = theDir + "" + clientName + "-Stats.pdf" Suggestions as to how to approach this? I tried the following code but since it is printing to the "Adobe pdf" printer, I get a "s

使用python 2.7.5将pdf文件打印成“Adobe pdf”打印机

我正尝试从python 2.7.5脚本中打印一个pdf文件到“Adobe pdf”打印机。 通过这样做,文件大小急剧减少。 文件名和路径是: pdfdoc = theDir +“”+ clientName +“-Stats.pdf” 有关如何解决这个问题的建议? 我尝试了下面的代码,但由于它打印到“Adobe pdf”打印机,我得到了“另存为”对话框,我不想要。 from subprocess import call acrobat = "C:Program Files (x86)AdobeAcrobat 10.0AcrobatAcrobat.exe" pdfdoc = theDir

How to sort contents in a file in python

I'm trying to figure out a simple way to sort words from a file, however the spaces "n" are always returned when I print the words. How could I improve this code to make it work properly? I'm using python 2.7 Thanks in advance. def sorting(self): filename = ("food.txt") file_handle = open(filename, "r") for word in file_handle: word = word.split()

如何在Python中对文件中的内容进行排序

我试图找出一个简单的方法来排序文件中的单词,但是当我打印这些单词时总会返回空格“ n”。 我如何改进这些代码以使其正常工作? 我使用Python 2.7提前致谢。 def sorting(self): filename = ("food.txt") file_handle = open(filename, "r") for word in file_handle: word = word.split() print sorted(file_handle) file_handle.close() 你在这里实际上有两个问题。 最大的问题是print

Python 2.7.5 installation on Ubuntu, MAXREPEAT

This has been asked a couple of times but the answers do not make any sense at least to me. I have Ubuntu 12.04 and had to upgrade Python from default (2.7.3?) to 2.7.5 to get libraries I can use to compile modwsgi. I obtained 2.7.5 sources, configured with --enable-shared, made and installed. A few obsolete sun audio etc libraries could not be built but otherwise the installation went throug

在Ubuntu上安装Python 2.7.5,MAXREPEAT

这已被问了几次,但答案至少对我来说没有任何意义。 我有Ubuntu 12.04,并且必须将Python从默认(2.7.3?)升级到2.7.5,以获取我可以用来编译modwsgi的库。 我获得了2.7.5源代码,配置了--enable-shared,制作和安装。 一些过时的太阳音频等库不能建成,否则安装过程就会结束。 除了它不起作用: visti@ubuntu:~$ python Traceback (most recent call last): File "/usr/local/lib/python2.7/site.py", line 548, in <

Modify file using python

I have files that contains the following lines: info face="asd" size=49 bold=0 italic=0 charset="" unicode=0 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=0,0 common lineHeight=52 base=43 scaleW=128 scaleH=128 pages=1 packed=0 page id=0 file="asd.png" chars count=9 char id=32 x=58 y=82 width=0 height=0 xoffset=0 yoffset=40 xadvance=9 page=0 chnl=0 char id=179 x=34 y=42 width=28 height=38 xo

使用python修改文件

我有包含以下行的文件: info face="asd" size=49 bold=0 italic=0 charset="" unicode=0 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=0,0 common lineHeight=52 base=43 scaleW=128 scaleH=128 pages=1 packed=0 page id=0 file="asd.png" chars count=9 char id=32 x=58 y=82 width=0 height=0 xoffset=0 yoffset=40 xadvance=9 page=0 chnl=0 char id=179 x=34 y=42 width=28 height=38 xoffset=2 yoffset=6 xadvanc

Relative imports for the billionth time

I've been here: http://www.python.org/dev/peps/pep-0328/ http://docs.python.org/2/tutorial/modules.html#packages Python packages: relative imports python relative import example code does not work Ultimate answer to relative python imports Relative imports in Python Python: Disabling relative import and plenty of URLs that I did not copy, some on SO, some on other sites, back

第十亿次相对进口

我来过这里: http://www.python.org/dev/peps/pep-0328/ http://docs.python.org/2/tutorial/modules.html#packages Python包:相对导入 python相对导入示例代码不起作用 相对python导入的最终答案 Python中的相对导入 Python:禁用相对导入 以及大量的我没有复制的网址,有些网址是在其他网站上,有些在我以为我很快就能找到解决方案。 永远再次出现的问题是:对于Windows 7,32位Python 2.7.3,我该如何解决

Check if a Python list item contains a string inside another string

I have a list: my_list = ['abc-123', 'def-456', 'ghi-789', 'abc-456'] and want to search for items that contain the string 'abc' . How can I do that? if 'abc' in my_list: would check if 'abc' exists in the list but it is a part of 'abc-123' and 'abc-456' , 'abc' does not exist on its own. So how can I get all items that contain 'abc' ? If yo

检查一个Python列表项是否包含另一个字符串内的字符串

我有一个列表: my_list = ['abc-123', 'def-456', 'ghi-789', 'abc-456'] 并且想要搜索包含字符串'abc' 。 我怎样才能做到这一点? if 'abc' in my_list: 会检查列表中是否存在'abc' ,但它是'abc-123'和'abc-456' , 'abc'不存在。 那么我怎样才能得到所有包含'abc'呢? 如果您只想检查列表中任何字符串中是否存在abc ,则可以尝试 some_list = ['abc-123', 'def-456