- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 19.2k
ENH/BUG: ignore line comments in CSV files GH2685 #4505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Closed
      
      
    
  
     Closed
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -991,7 +991,6 @@ def __init__(self, src, **kwds): | |
| self._name_processed = True | ||
| (index_names, self.names, | ||
| self.index_col) = _clean_index_names(self.names, self.index_col) | ||
|  | ||
| if self.index_names is None: | ||
| self.index_names = index_names | ||
|  | ||
|  | @@ -1100,7 +1099,6 @@ def _get_index_names(self): | |
| if self._reader.leading_cols == 0 and self.index_col is not None: | ||
| (idx_names, names, | ||
| self.index_col) = _clean_index_names(names, self.index_col) | ||
|  | ||
| return names, idx_names | ||
|  | ||
| def _maybe_parse_dates(self, values, index, try_parse_dates=True): | ||
|  | @@ -1282,21 +1280,30 @@ class MyDialect(csv.Dialect): | |
|  | ||
| sniff_sep = True | ||
|  | ||
| if sep is not None: | ||
| if (sep is not None) and (dia.quotechar is not None): | ||
| sniff_sep = False | ||
| dia.delimiter = sep | ||
| # attempt to sniff the delimiter | ||
| if sniff_sep: | ||
| line = f.readline() | ||
| while self.pos in self.skiprows: | ||
| self.pos += 1 | ||
| line = f.readline() | ||
|  | ||
| line = self._check_comments([line])[0] | ||
| line = self._check_comments([[line]]) | ||
|  | ||
| while not line: | ||
| self.pos += 1 | ||
| line = f.readline() | ||
| line = self._check_comments([[line]]) | ||
|  | ||
| line = line[0][0] | ||
|  | ||
| self.pos += 1 | ||
| sniffed = csv.Sniffer().sniff(line) | ||
| dia.delimiter = sniffed.delimiter | ||
| if not dia.delimiter: | ||
| dia.delimiter = sniffed.delimiter | ||
| if not dia.quotechar: | ||
| dia.quotechar = sniffed.quotechar | ||
| if self.encoding is not None: | ||
| self.buf.extend(list( | ||
| com.UnicodeReader(StringIO(line), | ||
|  | @@ -1466,14 +1473,26 @@ def _next_line(self): | |
| line = self.data[self.pos] | ||
| except IndexError: | ||
| raise StopIteration | ||
|  | ||
| line = self._check_comments([line]) | ||
|  | ||
| while not line: | ||
| self.pos += 1 | ||
| try: | ||
| line = self.data[self.pos] | ||
| except IndexError: | ||
| raise StopIteration | ||
| line = self._check_comments([line]) | ||
|  | ||
| line = line[0] | ||
| else: | ||
| while self.pos in self.skiprows: | ||
| next(self.data) | ||
| self.pos += 1 | ||
|  | ||
| line = next(self.data) | ||
| line = self._check_comments([line])[0] | ||
|  | ||
| line = self._check_comments([line])[0] | ||
| line = self._check_thousands([line])[0] | ||
|  | ||
| self.pos += 1 | ||
|  | @@ -1496,7 +1515,10 @@ def _check_comments(self, lines): | |
| if len(x) > 0: | ||
| rl.append(x) | ||
| break | ||
| ret.append(rl) | ||
| if rl: | ||
| ret.append(rl) | ||
| if not ret: | ||
| ret = [[]]; | ||
| return ret | ||
|  | ||
| def _check_thousands(self, lines): | ||
|  | @@ -1524,7 +1546,7 @@ def _clear_buffer(self): | |
| def _get_index_name(self, columns): | ||
| orig_names = list(columns) | ||
| columns = list(columns) | ||
|  | ||
| try: | ||
| line = self._next_line() | ||
| except StopIteration: | ||
|  | @@ -1539,7 +1561,7 @@ def _get_index_name(self, columns): | |
|  | ||
| # implicitly index_col=0 b/c 1 fewer column names | ||
| implicit_first_cols = 0 | ||
| if line is not None: | ||
| if line and (line is not None): | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here | ||
| # leave it 0, #2442 | ||
| if self.index_col is not False: | ||
| implicit_first_cols = len(line) - len(columns) | ||
|  | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need for parens here
is notbinds tighter thanand