python-2.6

Python readlines() usage and efficient practice for reading

隐身守侯 提交于 2019-12-17 02:28:22
问题 I have a problem to parse 1000's of text files(around 3000 lines in each file of ~400KB size ) in a folder. I did read them using readlines, for filename in os.listdir (input_dir) : if filename.endswith(".gz"): f = gzip.open(file, 'rb') else: f = open(file, 'rb') file_content = f.readlines() f.close() len_file = len(file_content) while i < len_file: line = file_content[i].split(delimiter) ... my logic ... i += 1 This works completely fine for sample from my inputs (50,100 files) . When I ran

how to write to a text file using python ?

梦想的初衷 提交于 2019-12-13 09:47:14
问题 I am trying to output a full for iteration. The output should be in a text file. How should I code for that ? The output should look like : Iteration 1 values --------> val1 < tab > val2 < tab > val3 Iteration 2 values --------> val4 < tab > val5 < tab > val6 This is not much of an information but I don't know where to start , can any1 show me plz ?? At the basic how should I open a text file and try and write using tabs ?? 回答1: numbers = [1,2,3,4,5,6] with open('output.txt', 'w') as f: f

Python module Import error for one user but not the other

落花浮王杯 提交于 2019-12-13 03:46:24
问题 This works: root@host#/usr/bin/python26 script.py Success This results in an error: user@host$/usr/bin/python26 script.py ImportError: No module named OpenSSL The users have identical PYTHONPATHs. Why does the 2nd user fail to import the module? 回答1: Ran chmod 755 on all directories in python path. Non-root user was able to execute script after this. 来源: https://stackoverflow.com/questions/46244850/python-module-import-error-for-one-user-but-not-the-other

Value error, truth error, ambiguous error

不问归期 提交于 2019-12-13 00:28:40
问题 When using this code for i in range(len(data)): if Ycoord >= Y_west and Xcoord == X_west: flag = 4 I get this ValueError if Ycoord >= Y_west and Xcoord == X_west: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() then i use the above restriction Any help on how i can keep my restriction and go on with the writing of my file? 回答1: The variables Ycoord and Xcoord are probably numpy.ndarray objects. You have to use the array compatible and

point from a list into a dicitonary variable

。_饼干妹妹 提交于 2019-12-12 16:36:58
问题 Assume you have a list a = [3,4,1] I want with this information to point to the dictionary: b[3][4][1] Now, what I need is a routine. After I see the value, to read and write a value inside b's position. I don't like to copy the variable. I want to change variable b's content directly. 回答1: Assuming b is a nested dictionary, you could do reduce(dict.get, a, b) to access b[3][4][1] . For more general object types, use reduce(operator.getitem, a, b) Writing the value is a bit more involved:

Shell expansion in Python subprocess [duplicate]

倖福魔咒の 提交于 2019-12-12 15:52:17
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Python subprocess wildcard usage Using the Python 2.6 subprocess module, I need to run a command on a src.rpm file that I am building with a previous subprocess call. Unfortunately, I am working with spec files that are not consistent, so I only have a vague idea of what the filename of the src.rpm should look like (for instance, I know the name of the package and the extension in something named "{package}-

How do i split a very long string into a list of shorter strings in python

二次信任 提交于 2019-12-12 15:15:35
问题 In my current django project I have a model that stores very long strings (can be 5000-10000 or even more characters per DB entry) and then i need to split them when a user is calling the record (it really need to be in one record in the DB). What i need is it to return a list (queryset? depends if in the "SQL" part or getting all the list as is and doing the parsing in the view) of shorter strings (100 - 500 characters per sting in the list i return to the template). I couldn't find anywhere

Automatically read configuration values in Python

故事扮演 提交于 2019-12-12 14:57:01
问题 I would like to read a configuration file in Python completely into a data structure without explicitly 'getting' each value. The reason for doing so is that I intend to modify these values programatically (for instance, I'll have a variable that says I want to modify [Foo] Bar = 1 to be [Foo] Bar = 2 ), with the intention of writing a new configuration file based on my changes. At present, I'm reading all the values by hand: parser = SafeConfigParser() parser.read(cfgFile) foo_bar1 = int

How to get pip to point to newer version of Python

隐身守侯 提交于 2019-12-12 10:43:49
问题 I have two versions of Python installed on my centOS server. [ethan@demo ~]$ python2.6 --version Python 2.6.6 [ehtan@demo ~]$ python --version Python 2.7.3 The older version (2.6) is required by some essential centOS packages so I can't remove it. When I install packages using pip , they are being installed in Python 2.6 . But instead I want them to be installed to Python 2.7 . How can I change this behaviour? For example, here is what happened when I tried installing Wand [ethan@demo ~]$ pip

Can't install Pillow on centos

强颜欢笑 提交于 2019-12-12 10:38:31
问题 I have cenots 6.3 and python 2.6 on it when I try to install it via easyinstall I get following error: _imaging.c:76:20: error: Python.h: No such file or directory In file included from /tmp/easy_install-HY7WI1/Pillow-2.3.0/libImaging/Imaging.h:14, from _imaging.c:82: /tmp/easy_install-HY7WI1/Pillow-2.3.0/libImaging/ImPlatform.h:14:2: error: #error Sorry, this library requires support for ANSI prototypes. /tmp/easy_install-HY7WI1/Pillow-2.3.0/libImaging/ImPlatform.h:17:2: error: #error Sorry,