Tuesday, 15 June 2010

python - adding 4 to an element of a list (that I tried to convert to an integer) -


If the 6th digit of each line in my file is 2, then I want to add 4 to 10 elements on the same line. I am running into problems with extras.

  Import CSV PlusFur = [] with Open ("101.txt", 'rb') as f: reader = csv.reader (f) # for each row in the data Data = List for Reader # for i = 0: If the data is [Deathline] [5] is "2": PlusFur.Append (Int (Data [Deathline] [9]]) + 4) Print (PlusFor) # Print (Data [Deathline] [9]) Datlin + = 1  

But it will not print anything. I also tried

  if the data [death] [5] is "2": print (data [deathline] [9])  

and it So, with this issue I am trying to add 4 to that value. I do not know what I'm doing wrong in specifying the data [ડેટिन]] [9] can be added as an integer.

Actually, if the value of a column in my CSV is 2, how can I add 4 to the value of the second column on the same row? I need to be repeated through each line.

Here is the part of the file I am loading: input file

Here is the output of my 2 code snippet: Out of my 2 code snippet output

< P> Edit: I had some problems because some of my values ​​were empty I have now:

  import csv plusfour = [] # open in open file ("101.txt", ' Rb ') file with all topics: reader = csv.reader (f) # Data for each data = notification (Reader) # data in a row for it = dataline = 0 valid_responses = ("1", "2"): If the data is not in [death] [5] Validities: PlusFor.Append ("NA") Alief Int (Float (data [datalines]] [5]) == 2: add value to the value of 10th element in the same row data [decaline] [9] = int (float (data [deathline] [9])) + 4PlusFor.Append (Data [Deathline] [9]) Print (PlusFor) Alief Int (Float (Data [Deathline] [5])) == 1: PlusFur.Append (data [Dateline] [9]) dataline + 1  

and this error Getting started: data [date] [9] = int (float (data [deathline] [9]) 4 value error: could not convert string to float:

Problem:

Create new column in input file as per RU, there are some rules

  1. If the 5th value is blank or is not a number, you can only enter something in "NA" or new variable.
  2. If the 5th and the 9th is not empty or the number is, then you can only enter "NA" in the new variable
  3. If the 5th is the 2nd and 9th integers, then You can add 4 to it.
  4. Alo:

    1. Open the input file for reading and open output file write mode.
    2. Input the file for the line in the reader i.e. :
    3. Get 5th item per line Exception Handling Casting
    4. with the exception of step 3 if the ValueError exceptions add new column value
    5. if the 5th If the value of the object is equal to 2 then the value of the 9th object meets the threshold.
    6. To get 9th item, type the casting with the exception handling.
    7. If step 6 is extended, then ValueError exception then add new column value NA then type on the output file and continue to the next line Repeat
    8. Otherwise add 4 to the value of 9th item and type in the output file.
    9. We can also do this for other target values.

        code:

         : reader    with open ("101.txt", 'rb') = Csv.reader (fp1) # - Open output file as open ('101_output.csv', 'wb') fp2: writer = csv.writer (fp2, delimiter = ',') # - For example: try: item_5 = int (line [5]) Exclude ValueError: # - Copy the line new_row = list (line) # - Add 9th value new_row.append ("NA") # - Row Writer Write .writerow (new_row) Print "Error 1: Type of Casting Type Item2 == 2: # 10th in the same line Try adding 4 to element value: item_13 = int (line [9]) + 4 new_row.append (item_13)% s "% line [5] new_RO = continue the list (line)) except ValueError: new_row.append ("NA") elif item_5 == 1: # Continue the data [death] [9] Try the same: item_13 = int (line [9]) + 0 new_row.append (item_13) Exclude ValueError: new_row .append ("NA") and: Add new value from the 13th item of # 9th itemNew_row.append (line [9]) writer.writerow (new_row)  

No comments:

Post a Comment