skip

Python: How to ignore #comment lines when reading in a file

耗尽温柔 提交于 2019-11-27 17:58:55
In Python, I have just read a line form a text file and I'd like to know how to code to ignore comments with a hash # at the beginning of the line. I think it should be something like this: for if line !contain # then ...process line else end for loop But I'm new to Python and I don't know the syntax you can use startswith() eg for line in open("file"): li=line.strip() if not li.startswith("#"): print line.rstrip() I recommend you don't ignore the whole line when you see a # character; just ignore the rest of the line. You can do that easily with a string method function called partition :

Skipping execution of -with- block

大憨熊 提交于 2019-11-27 10:55:26
问题 I am defining a context manager class and I would like to be able to skip the block of code without raising an exception if certain conditions are met during instantiation. For example, class My_Context(object): def __init__(self,mode=0): """ if mode = 0, proceed as normal if mode = 1, do not execute block """ self.mode=mode def __enter__(self): if self.mode==1: print 'Exiting...' CODE TO EXIT PREMATURELY def __exit__(self, type, value, traceback): print 'Exiting...' with My_Context(mode=1):

How can I read the header but also skip lines - read.table()?

≡放荡痞女 提交于 2019-11-27 07:49:04
Data.txt: Index;Time; 1;2345; 2;1423; 3;5123; The code: dat <- read.table('data.txt', skip = 1, nrows = 2, header =TRUE, sep =';') The result: X1 X2345 1 2 1423 2 3 5123 I expect the header to be Index and Time, as follows: Index Time 1 2 1423 2 3 5123 How do I do that? Beasterfield I am afraid, that there is no direct way to achieve this. Either you read the entire table and remove afterwards the lines you don't want or you read in the table twice and assign the header later: header <- read.table('data.txt', nrows = 1, header = FALSE, sep =';', stringsAsFactors = FALSE) dat <- read.table(

Python: How to ignore #comment lines when reading in a file

六眼飞鱼酱① 提交于 2019-11-27 04:15:12
问题 In Python, I have just read a line form a text file and I'd like to know how to code to ignore comments with a hash # at the beginning of the line. I think it should be something like this: for if line !contain # then ...process line else end for loop But I'm new to Python and I don't know the syntax 回答1: you can use startswith() eg for line in open("file"): li=line.strip() if not li.startswith("#"): print line.rstrip() 回答2: I recommend you don't ignore the whole line when you see a #

LINQ with Skip and Take

大城市里の小女人 提交于 2019-11-27 03:51:14
问题 I used the below code to take some items from IEnumerable , but it is always returning the source as null and count as 0 and actually there are items exists in IEnumerable private void GetItemsPrice(IEnumerable<Item> items, int customerNumber) { var a = items.Skip(2).Take(5); } When i try to access a it has count 0 . Anything goes wrong here? 回答1: Remember, that variable a in your code is a query itself . It is not result of query execution . When you are using Immediate Window to watch query

Skip to Previous AVPlayerItem on AVQueuePlayer / Play selected Item from queue

。_饼干妹妹 提交于 2019-11-27 02:45:48
问题 I am playing a Tv-show that has been sliced to different chapters on my project using an AVQueuePlayer. I also want to offer the possibility to skip to the previous/next chapter or to select a different chapter on the fly, while the AVQueuePlayer is already playing. Skipping to next Item is no problem with the advanceToNextItem provided by AVQueuePlayer, but there is nothing alike for skipping back or playing a certainitem from the queue. So I am not quite sure what would be the best approach

Print a file skipping first X lines in Bash

时光总嘲笑我的痴心妄想 提交于 2019-11-27 02:21:19
I have a very long file which I want to print but skipping the first 1e6 lines for example. I look into the cat man page but I did not see any option to do this. I am looking for a command to do this or a simple bash program. SingleNegationElimination You'll need tail. Some examples: $ tail great-big-file.log < Last 10 lines of great-big-file.log > If you really need to SKIP a particular number of "first" lines, use $ tail -n +<N+1> <filename> < filename, excluding first N lines. > That is, if you want to skip N lines, you start printing line N+1. Example: $ tail -n +11 /tmp/myfile < /tmp

Skip first couple of lines while reading lines in Python file

别等时光非礼了梦想. 提交于 2019-11-26 23:52:32
I want to skip the first 17 lines while reading a text file. Let's say the file looks like: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 good stuff I just want the good stuff. What I'm doing is a lot more complicated, but this is the part I'm having trouble with. wim Use a slice, like below: with open('yourfile.txt') as f: lines_after_17 = f.readlines()[17:] If the file is too big to load in memory: with open('yourfile.txt') as f: for _ in range(17): next(f) for line in f: # do stuff Ismail Badawi Use itertools.islice , starting at index 17. It will automatically skip the 17 first lines. import itertools

Maven - skip parent project build

感情迁移 提交于 2019-11-26 23:28:01
问题 I know it's mauvais ton to ask twice in a single day but here's another Maven puzzler: I have a parent POM which defines 5 modules (5 subprojects). Since each module is executed in exactly the same way I pull <profile><build> section into the parent POM to get rid of the duplicate code. Now - if I execute build individually from each module it works, however if I want to build all modules at once and move to the parent directory I got error since the very first thing Maven tries to execute is

read.csv, header on first line, skip second line [duplicate]

独自空忆成欢 提交于 2019-11-26 18:49:37
This question already has an answer here: How can I read the header but also skip lines - read.table()? 5 answers I have a CSV file with two header rows, the first row I want to be the header, but the second row I want to discard. If I do the following command: data <- read.csv("HK Stocks bbg.csv", header = T, stringsAsFactors = FALSE) The first row becomes the header and the second row of the file becomes the first row of my data frame: Xaaaaaaaaa X X.1 Xbbbbbbbbbb X.2 X.3 1 Date PX_LAST NA Date PX_LAST NA 2 31/12/2002 38.855 NA 31/12/2002 19.547 NA 3 02/01/2003 38.664 NA 02/01/2003 19.547 NA